Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:23:08+00:00 2026-05-30T22:23:08+00:00

I’m a rails beginner (I think I have the basics down pretty well at

  • 0

I’m a rails beginner (I think I have the basics down pretty well at this point), and I’m working on an app with a simple parent-child structure: Company => Client => Group => Order. The relevant DB tables for this question are Group and Order (Group has_many Orders).

Right now, when I create a new group, one of the text fields on the form is ‘Order Quantity’ (which is a :size attribute in the Group model), and there is a line of code in the Create action for GroupsController that generates n blank orders that belong to that particular group:

  if @group.save
    @group.size.times do
      Order.create(:group_id => @group.id)
    end

This works fine, but I want Order.id to be a decimal number rather than an integer. I tried generating the following migration:

class ChangeOrderIdToDecimal < ActiveRecord::Migration
  def up
    change_column :orders, :id, :decimal, :precision => 5, :scale => 4
  end

which, at first, seemed to do what I wanted. All the existing orders changed from 4, 5, 6 to 4.0, 5.0, 6.0. What I ultimately want to do, though, is to add some code to the Create action in GroupsController like this:

  if @group.save
    order_id_decimal = 0.001
    @group.size.times do
      order_id = "#{@group.id}.#{order_id_decimal}".to_f
      Order.create(:group_id => @group.id, :id => order_id)
      order_id_decimal += 0.001
    end

The idea being, if the Group ID was 75, then the orders in that Group would have ID’s 75.001, 75.002, 75.003, etc.

For some reason, though, when I create new groups, it tries to save every order id as 0.0000 and gives me this error:

ActiveRecord::RecordNotUnique in GroupsController#create

Mysql2::Error: Duplicate entry '0.0000' for key 'PRIMARY': INSERT INTO `orders`
(`created_at`, `group_id`, `note`, `order_status`, `title`, `updated_at`, ) VALUES 
('2012-03-04 20:28:15', 20, NULL, NULL, NULL, '2012-03-04 20:28:15')

I’ve tried manually changing an Order’s ID from the rails console, and it saves without giving me errors, but it doesn’t actually save. If I type Order.find(0.0) it pulls up the one order I just changed and it has 0.0 for ID. I’m completely baffled by this, and I haven’t been able to find any help from googling it. Anyone have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:23:09+00:00Added an answer on May 30, 2026 at 10:23 pm

    You’re trying to encapsulate business logic in the ID field. This is a big no-no from an application design perspective, and having an ID field that isn’t an integer is a big no-no from a database design perspective.

    I would give up on this notion that you want the IDs to be somehow correlated across tables; foreign keys and join tables should be able to accomplish the end goal (of relating groups and orders). Primary keys are not intended for business logic and should not be used for that: don’t care about the contents of your ID fields and your application will be a lot happier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.