That's pretty nice. It seems like a much neater idea to always just define it on the model with the foreign key, because after all, the has_many is really just the same relationship, implied in the other direction.
Ben Hoskings On 12/01/2009, at 4:53 PM, Clifford Heath wrote: > > In ActiveFacts you have a one_to_one (sorta what has_one is, but > symmetrical, > you just say it on one end and you get the other end for free), and > has_one, > which is sort what belongs_to is, except it also creates the symmetric > has_many > on the other model. > > You never need to say has_many, it comes for free. > > Clifford Heath. > > On 12/01/2009, at 5:41 PM, Ben Hoskings wrote: > >> Yep. >> >> If it's a 1-n relationship (as opposed to an n-n), the model with >> the foreign key is the one that belongs_to the other model. >> >> Ben Hoskings >> >> >> On 12/01/2009, at 4:06 PM, Ryan Bigg wrote: >> >>> A status in your example would "has_many" comments, as many >>> comments can have the same status. Therefore, a comment would >>> belongs_to a status. >>> ----- >>> Ryan Bigg >>> Freelancer >>> http://frozenplague.net >>> >>> >>> >>> >>> >>> >>> >>> On 10/01/2009, at 11:41 AM, Matt wrote: >>> >>>> That's not a great case for the has_one association because as you >>>> pointed out, the real world usage probably isn't there. >>>> >>>> Here's a couple for you >>>> >>>> class Student < ActiveRecord::Base >>>> # so in here, there's an id, name for the student and any other >>>> details they may have >>>> has_one :locker >>>> end >>>> >>>> class Locker < ActiveRecord::Base >>>> # here you have the fields id, locker_number, student_id (this >>>> is the one for the association) >>>> belongs_to :student >>>> end >>>> >>>> --- >>>> >>>> class Sale < ActiveRecord::Base >>>> # id and whatever other fields >>>> has_one :invoice >>>> end >>>> >>>> class Invoice < ActiveRecord::Base >>>> # id, sale_id, any other fields you want >>>> belongs_to :sale >>>> end >>>> >>>> Do they help at all? >>>> >>>> Matt Didcoe >>>> [email protected] >>>> >>>> >>>> On Sat, Jan 10, 2009 at 4:54 AM, sohdubom <[email protected]> >>>> wrote: >>>> >>>> as an example i thought that the following case could be a good one >>>> for has_one relationship, but i ended up very confused: >>>> >>>> - suppose i have a Comment model with attributes: author, content, >>>> status (just 3 to simplify) >>>> >>>> - i wouldn't do that in a real scenario, but let's say i don't >>>> want to >>>> create enums for status and decide to normalize it by creating an >>>> extra model: Status >>>> >>>> in this case: a comment has_one status (like new, ok, bad, ...), so >>>> the Comment model will have as fk: status_id and status belongs_to >>>> comment (in singular) or status belongs_to comments (plural) ? >>>> >>>> >>>> >>>> >>>> >>>> >>> >>> >>> >>> >> >> >>> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" 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/rails-oceania?hl=en -~----------~----~----~----~------~----~------~--~---
