Hello,
I’m fairly new to Ruby, but I am trying to understand the relationship
between database structure and models.
My difficulty is that I have five or so tables all linked together and
their
primary key column is a serial number
The tables are a set of tables called:
itemmasters
products
prices
subprices
imprints
etc.
products, prices, and subprices, imprints, etc. belong to itemmasters
based
on a serial number
How can I get about linking them without having to have a column in
itemmasters for each of the sub tables?
For instance:
product_id
price_id
subprice_id
imprint_id
This would be somewhat silly as they all contain the same information.
Right?
Thank you,
Brian A.
On Jun 30, 2008, at 9:17 AM, Brian A. wrote:
The tables are a set of tables called:
on a serial number
This would be somewhat silly as they all contain the same information.
Right?
You could try playing around with primary_key() to set the primary key
for the given table as well as adjusting foreign_key in your has_one
relationships to point to that primary key. Not sure if that will get
you into trouble or not 
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
relationships.
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.
On Jun 30, 11:17 am, Brian A. [email protected]
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
table.
If you are just doing a straight select from a single table try using
all lower case?
On Jun 30, 12:18Â pm, Brian A. [email protected]
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])
end
end
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.
-Brian
Jorg L. wrote:
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
relationships.
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.
On Jun 30, 11:17�am, Brian A. [email protected]
@Brian:
Your code says:
class ItempriceController < ApplicationController
def show
@itemprices = Itemprice.find(params[:ItemSerial])
end
end
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])
end
end
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.