Hi. On the document the has_many :limit option : The :limit option lets you restrict the total number of objects that will be fetched through an association. I'm a little confused about this explanation. is fetched means it can save more but restircted numbers to retrieve at once? For example, we can have wife/husband up to 1 (unless arab..) I suppose class Me < ActiveRecord::Base has_many :wife end because i can have a wife or not. so it is not 1:1, isn't it? but at the same time i want to limit the number of wife to just 1. how can i represent this relationship into active record? Thanks.
on 2009-04-30 13:26
on 2009-04-30 13:43
Use has_one :wife This allows 0 or 1 wife unless your validations require that a wife exists See http://guides.rubyonrails.org/association_basics.html for an explanation of the various associations. 2009/4/30 serenobs <email@example.com>
on 2009-04-30 13:57
2009/4/30 Colin L. <firstname.lastname@example.org> > Use > has_one :wife On second thoughts I am not sure about this, writing class Wife < ActiveRecord::Base belongs_to :husband end may well get you into serious trouble.
on 2009-04-30 18:16
Colin L. wrote: > 2009/4/30 Colin L. <email@example.com> > >> Use >> has_one :wife > > > On second thoughts I am not sure about this, writing > > class Wife < ActiveRecord::Base > belongs_to :husband > end > > may well get you into serious trouble. This is actually one of the more interesting examples. In the specific case of husband and wife the design pattern that most people use is something like the following: Story: Assuming the relationship is based on one spouse at a time a many-to-many relationship is still used to track the history of marriages between people. The one-spouse-at-a-time rule would then be implemented in validation code. Person < ActiveRecord::Base has_many :marriages has_many :spouses, :through => :marriage validate :one_spouse_at_a_time def current_spouse # find and return the person's current spouse end protected def one_spouse_at_a_time # do the validation here end end Something along that line anyway.
on 2009-05-01 02:51
That reminded me of a really funny article: "Gay marriage: the database engineering perspective" http://qntm.org/?gay Amusing, regardless of one's opinion of the issue. --Matt J. On Apr 30, 9:16 am, Robert W. <firstname.lastname@example.org>