Tables: Unnecessary duplication of id fields

In third normal relational design you would have the itemmaster_id in
each of the dependent tables.
That is also what happens when you define the belongs_to has_many
There needs to be some column in the database to relate the items.

What you bring up, each child id on the parent table is a star schema.

Otherwise you have to denormalize your data design and just stuff
everything into one table.

Thanks, everyone for the responses. I think I am clearly conceptualizing
things wrongly.

All the tables have an ItemSerial field

I am getting an error saying "Couldn't find Itemprice without an ID"

This message comes up even despite the fact that this is my controller:

class ItempriceController < ApplicationController
  def show
    @itemprices = Itemprice.find(params[:ItemSerial])

I am not finding Itemprice by id according to the above, but still I get
this error.

In my view as a test I have the following:

<% @page_title = "#{@itemprices.Price1}" %>

I may be thinking about this whole thing wrong.


Jorg Lueke wrote:

Rails expects the ID name to be a certain way, so if you use a
different ID you have to map those columns.
For a parent child relationship where the child belongs_to a given
parent. The default ID rais expects is parent_id.
For HABTM relationships you would need to create an intermediate

If you are just doing a straight select from a single table try using
all lower case?


Your code says:

class ItempriceController < ApplicationController

def show

@itemprices = Itemprice.find(params[:ItemSerial])



The .find method on an ActiveRecord model finds by primary key. Without mapping your primary keys (either by doing it the way Rails wants you to or by overriding the primary key using the set_primary_key method, Rails will try to find by id.

Each field in your table also gets two finders automatically created for it. In your case, you have a field called item_serial_number or something simlar… let’s call it serialnumber for this example. With that field, Rails creates two new methods on the class:

Itemprice.find_by_serialnumber(15) (“select * from itemprice where serialnumber = 15 LIMIT 1”)
Itemprice.find_all_by_serialnumber(15) (“select * from itemprice where serialnumber = 15”)

So to get what you want, just do

class ItempriceController < ApplicationController

def show

@itemprices = Itemprice.find_by_serialnumber(params[:ItemSerial])


You should grab some books on Ruby and Rails though… Rails can work with “legacy” schemas but it’s much more difficult if you’re just starting out. There are a lot of database conventions that Rails expects you to adhere to.

Good luck and I hope this helped a bit.