The Barge wrote: > I think I've just thoroughly confused myself at this point. I have a > class that has a has_many and has_one relationship that use the same > object. > > Here is an example: > > class Person < ActiveRecord::Base > has_many :hobbies > has_one :favorite_hobby, :class_name => "Hobby" > end > > class Hobby < ActiveRecord::Base > belongs_to :person > belongs_to :person, :foreign_key => "favorite_hobby_id" > end > > Of course, this doesn't work. Any ideas what I need to change to get > this to work properly? > > Thanks.
I think you should be using habtm for the hobbies (two people can enjoy the same hobby, right?) and an additional belongs_to for the favorite. This may seem counterintuitive, but remember that belongs_to is used on the table with the foreign key field (which in this case is people.favorite_hobby_id). So: class Person habtm :hobbies belongs_to :favorite_hobby, :class_name => 'Hobby' end class Hobby habtm :people # reverse association probably unnecessary for favorite_hobby end Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

