Forum: Ruby on Rails is possible limit the number of has_many objects?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67259181b4d628f350f5edfafcea6520?d=identicon&s=25 serenobs (Guest)
on 2009-04-30 11:26
(Received via mailing list)
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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-04-30 11:43
(Received via mailing list)
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 <serenobs@gmail.com>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-04-30 11:57
(Received via mailing list)
2009/4/30 Colin Law <clanlaw@googlemail.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.
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-04-30 16:16
Colin Law wrote:
> 2009/4/30 Colin Law <clanlaw@googlemail.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.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-05-01 00:51
(Received via mailing list)
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 Jones

On Apr 30, 9:16 am, Robert Walker <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.