Has_many belongs_to foreign key question

i have a 2 tables

ITEMS
id
name
attribute_id
attribute_secondary_id

ATTRIBUTES
id
name

what i am trying to do is have attribute_id and attribute_secondary_id
both link to the ATTRIBUTES tables. At the moment attribute_id works
fine but I’m not sure how i should do the attribute_secondary_id.

my models

ITEMS
belongs_to :attribute
belongs_to :attribute, :foreign_key => ‘attribute_secondary_id’ (won’t
work)

ATTRIBUTES
has_many :items

i want to do something like the following

item.attribute.name (for primary attribute)
item.attribute_secondary_id.name (not working…how should i do this?)

i don’t really want to set up another table ITEM_ATTRIBUTES. Is there a
way to do this as i mentioned?

On Jun 19, 8:53 am, Scott K. [email protected]
wrote:

ITEMS
belongs_to :attribute
belongs_to :attribute, :foreign_key => ‘attribute_secondary_id’ (won’t
work)

You’re close, but you just need to change the first parameter so you
can distinguish between the two:

belongs_to :attribute
belongs_to :secondary, :foreign_key => ‘attribute_secondary_id’

The you can do something like this:

item = Item.find(1)
puts item.attribute
puts item.secondary # uses attribute_secondary_id column to find the
associated item

This should work for you (or let me know if I misunderstood the
question.)

Jeff
www.purpleworkshops.com

On 19 Jun 2008, at 14:53, Scott K. wrote:

id
name

what i am trying to do is have attribute_id and attribute_secondary_id
both link to the ATTRIBUTES tables. At the moment attribute_id works
fine but I’m not sure how i should do the attribute_secondary_id.

I gave an example of this at

Fred

Thanks everyone! I got it working:

belongs_to :attribute_primary, :class_name => 'Attribute', 

:foreign_key => ‘attribute_id’
belongs_to :attribute_secondary, :class_name => ‘Attribute’,
:foreign_key => ‘attribute_secondary_id’