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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 19, 8:53 am, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 www.softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 19 Jun 2008, at 14:53, Scott Kulik wrote:> > 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. >I gave an example of this at http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table Fred> 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? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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'' -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---