# Tables: Unnecessary duplication of id fields

**URL:** <https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799>\
**Category:** Ruby\
**Created:** [June 30, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799 "2008-06-30T18:12:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [June 30, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/1 "2008-06-30T18:12:30Z")

</div>

=begin

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.

=end

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [June 30, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/2 "2008-06-30T18:33:00Z")

</div>

On Mon, Jun 30, 2008 at 11:09 AM, brian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The tables are a set of tables called:
> 
> This would be somewhat silly as they all contain the same information.  
> Right?
> 
> Thank you,
> 
> Brian A.
> 
> =end

While not really a Ruby or Rails question, it seems you have  
rows/tuples that point to a singular row in another table. In other  
words, reverse it. Have columns in your “subtables” that is the  
foreign key of the “master” one.

It’s possible I misread your question, though. I do that pretty  
frequently 🙂

Todd

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [June 30, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/3 "2008-06-30T19:14:07Z")

</div>

=begin  
Thank you for the response.

Maybe it will be clearer if I will just begin with the symptom.

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}” %\>

-Brian

----- Original Message -----  
From: “Todd B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, June 30, 2008 11:29 AM  
Subject: Re: Tables: Unnecessary duplication of id fields

> 

=end

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [June 30, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/4 "2008-06-30T22:42:38Z")

</div>

On Mon, Jun 30, 2008 at 12:10 PM, brian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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}” %\>
> 
> -Brian

Hmm. I’m no Rails expert, but maybe you should check the type/class  
of the object params[:ItemSerial]. Just a thought. Also, your  
database setup may be strange for Rails as I alluded to earlier. I’ve  
actually had this error a few times before, but can’t remember how I  
fixed it 😕

Todd

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [July 8, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/5 "2008-07-08T17:03:42Z")

</div>

Thanks all for the response. I will have to play with it some more this  
week, and update with how it goes…

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [June 30, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/tables-unnecessary-duplication-of-id-fields/141799/6 "2008-06-30T23:13:39Z")

</div>

On Mon, Jun 30, 2008 at 2:38 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end  
> > -Brian
> 
> Hmm. I’m no Rails expert, but maybe you should check the type/class  
> of the object params[:ItemSerial]. Just a thought. Also, your  
> database setup may be strange for Rails as I alluded to earlier. I’ve  
> actually had this error a few times before, but can’t remember how I  
> fixed it 😕
> 
> Todd

It sounds to me like Itemprice.find(params[:ItemSerial]) is returning  
nil  
for whatever reason. This is getting passed along and then rails is  
trying  
to retrieve the item from the database using nil for the id in your  
view.

If you are doing a find with one argument rails assumes that argument  
evaluates to an id value. If you want to match on a field you would  
need  
something like find(:all, :conditions =\> [“serial = ?”,  
params[:ItemSerial]])

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)
