(my post was deleted i don''t know why) Hi. I have a question. for example, when model records about man, suppose man can marry with 1 woman. then i think code will be like this class Man < ActiveRecord::Base has_many: wife end Because one man can marry with up to 1 woman( 0 or 1 ) it can''t be 1:1 relationship, didn''t it? but at the same time more than 1 wife is not allowed. then how can i model this relationship into rails code? has_many :limit option is fit for this relation? Thanks. and i wish it is not deleted again. If it should be deleted please let me know why it should be.
Commander Johnson
2009-Apr-30 16:56 UTC
Re: is possbile limit the number of object in has_many option?
Did you consider has_one :wife And in Wife.rb belongs_to :man On Thu, Apr 30, 2009 at 6:49 PM, serenobs <serenobs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > (my post was deleted i don''t know why) > Hi. I have a question. > for example, when model records about man, suppose man can marry with > 1 woman. > then i think code will be like this > class Man < ActiveRecord::Base > has_many: wife > end > > Because one man can marry with up to 1 woman( 0 or 1 ) > it can''t be 1:1 relationship, didn''t it? > but at the same time more than 1 wife is not allowed. > then how can i model this relationship into rails code? > has_many :limit option is fit for this relation? > > Thanks. > and i wish it is not deleted again. If it should be deleted please let > me know why it should be. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2009-Apr-30 17:17 UTC
Re: is possbile limit the number of object in has_many option?
On Apr 30, 2009, at 12:56 PM, Commander Johnson wrote:> Did you consider > > has_one :wife > > And in Wife.rb > > belongs_to :manOr: class Wife has_many :marriages has_one :current_marriage, :class_name => ''Marriage'', :conditions => { :current => true } has_one :husband, :through => :current_marriage end class Marriage belongs_to :wife belongs_to :husband end class Husband has_many :marriages has_one :current_marriage, :class_name => ''Marriage'', :conditions => { :current => true } has_one :wife, :through => :current_marriage end -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> On Thu, Apr 30, 2009 at 6:49 PM, serenobs <serenobs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > (my post was deleted i don''t know why) > Hi. I have a question. > for example, when model records about man, suppose man can marry with > 1 woman. > then i think code will be like this > class Man < ActiveRecord::Base > has_many: wife > end > > Because one man can marry with up to 1 woman( 0 or 1 ) > it can''t be 1:1 relationship, didn''t it? > but at the same time more than 1 wife is not allowed. > then how can i model this relationship into rails code? > has_many :limit option is fit for this relation? > > Thanks. > and i wish it is not deleted again. If it should be deleted please let > me know why it should be. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
serenobs
2009-Apr-30 17:29 UTC
Re: is possbile limit the number of object in has_many option?
thanks for replies. it gives me some thought about making active record association. then what if the man is a single? husband may not have current_marriage. i was thinking that has_one means it should have one object. But as you know not every man will marry. please more advice for this novice. On May 1, 2:17 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Apr 30, 2009, at 12:56 PM, Commander Johnson wrote: > > > Did you consider > > > has_one :wife > > > And in Wife.rb > > > belongs_to :man > > Or: > class Wife > has_many :marriages > has_one :current_marriage, :class_name => ''Marriage'', :conditions > => { :current => true } > has_one :husband, :through => :current_marriage > end > > class Marriage > belongs_to :wife > belongs_to :husband > end > > class Husband > has_many :marriages > has_one :current_marriage, :class_name => ''Marriage'', :conditions > => { :current => true } > has_one :wife, :through => :current_marriage > end > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > > On Thu, Apr 30, 2009 at 6:49 PM, serenobs <seren...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > (my post was deleted i don''t know why) > > Hi. I have a question. > > for example, when model records about man, suppose man can marry with > > 1 woman. > > then i think code will be like this > > class Man < ActiveRecord::Base > > has_many: wife > > end > > > Because one man can marry with up to 1 woman( 0 or 1 ) > > it can''t be 1:1 relationship, didn''t it? > > but at the same time more than 1 wife is not allowed. > > then how can i model this relationship into rails code? > > has_many :limit option is fit for this relation? > > > Thanks. > > and i wish it is not deleted again. If it should be deleted please let > > me know why it should be.- Hide quoted text - > > - Show quoted text -
Rob Biedenharn
2009-Apr-30 18:08 UTC
Re: is possbile limit the number of object in has_many option?
On Apr 30, 2009, at 1:29 PM, serenobs wrote:> > thanks for replies. > it gives me some thought about making active record association. > > then what if the man is a single? > husband may not have current_marriage. > i was thinking that has_one means it should have one object. > But as you know not every man will marry. > > please more advice for this novice.Then, man.current_marriage will be nil as will man.wife has_one really defines a 0/1 and has_many a 0/n relation. -Rob> > > > On May 1, 2:17 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote: >> On Apr 30, 2009, at 12:56 PM, Commander Johnson wrote: >> >>> Did you consider >> >>> has_one :wife >> >>> And in Wife.rb >> >>> belongs_to :man >> >> Or: >> class Wife >> has_many :marriages >> has_one :current_marriage, :class_name => ''Marriage'', :conditions >> => { :current => true } >> has_one :husband, :through => :current_marriage >> end >> >> class Marriage >> belongs_to :wife >> belongs_to :husband >> end >> >> class Husband >> has_many :marriages >> has_one :current_marriage, :class_name => ''Marriage'', :conditions >> => { :current => true } >> has_one :wife, :through => :current_marriage >> end >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org >> >> >> >>> On Thu, Apr 30, 2009 at 6:49 PM, serenobs <seren...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >> >>> (my post was deleted i don''t know why) >>> Hi. I have a question. >>> for example, when model records about man, suppose man can marry >>> with >>> 1 woman. >>> then i think code will be like this >>> class Man < ActiveRecord::Base >>> has_many: wife >>> end >> >>> Because one man can marry with up to 1 woman( 0 or 1 ) >>> it can''t be 1:1 relationship, didn''t it? >>> but at the same time more than 1 wife is not allowed. >>> then how can i model this relationship into rails code? >>> has_many :limit option is fit for this relation? >> >>> Thanks. >>> and i wish it is not deleted again. If it should be deleted please >>> let >>> me know why it should be.- Hide quoted text - >> >> - Show quoted text - > >
Chris Bird
2009-May-01 13:32 UTC
Re: is possbile limit the number of object in has_many option?
has_one does indeed mean has a maximum of one. The belongs_to side means has exactly one. I don''t know (because I never tried it) wjay happens if you put "belongs_to on both sides - i.e. make something exactly 1:1. Obviously not relevant in this case, but I could see situations where for performance reasons you may want that kind of partition - or when wrapping an external database. C On Apr 30, 1:08 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Apr 30, 2009, at 1:29 PM, serenobs wrote: > > > > > thanks for replies. > > it gives me some thought about making active record association. > > > then what if the man is a single? > > husband may not have current_marriage. > > i was thinking that has_one means it should have one object. > > But as you know not every man will marry. > > > please more advice for this novice. > > Then, man.current_marriage will be nil as will man.wife > > has_one really defines a 0/1 and has_many a 0/n relation. > > -Rob > > > > > > > On May 1, 2:17 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote: > >> On Apr 30, 2009, at 12:56 PM, Commander Johnson wrote: > > >>> Did you consider > > >>> has_one :wife > > >>> And in Wife.rb > > >>> belongs_to :man > > >> Or: > >> class Wife > >> has_many :marriages > >> has_one :current_marriage, :class_name => ''Marriage'', :conditions > >> => { :current => true } > >> has_one :husband, :through => :current_marriage > >> end > > >> class Marriage > >> belongs_to :wife > >> belongs_to :husband > >> end > > >> class Husband > >> has_many :marriages > >> has_one :current_marriage, :class_name => ''Marriage'', :conditions > >> => { :current => true } > >> has_one :wife, :through => :current_marriage > >> end > > >> -Rob > > >> Rob Biedenharn http://agileconsultingllc.com > >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > >>> On Thu, Apr 30, 2009 at 6:49 PM, serenobs <seren...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>> wrote: > > >>> (my post was deleted i don''t know why) > >>> Hi. I have a question. > >>> for example, when model records about man, suppose man can marry > >>> with > >>> 1 woman. > >>> then i think code will be like this > >>> class Man < ActiveRecord::Base > >>> has_many: wife > >>> end > > >>> Because one man can marry with up to 1 woman( 0 or 1 ) > >>> it can''t be 1:1 relationship, didn''t it? > >>> but at the same time more than 1 wife is not allowed. > >>> then how can i model this relationship into rails code? > >>> has_many :limit option is fit for this relation? > > >>> Thanks. > >>> and i wish it is not deleted again. If it should be deleted please > >>> let > >>> me know why it should be.- Hide quoted text - > > >> - Show quoted text -- Hide quoted text - > > - Show quoted text -
Rob Biedenharn
2009-May-01 18:12 UTC
Re: is possbile limit the number of object in has_many option?
On May 1, 2009, at 9:32 AM, Chris Bird wrote:> has_one does indeed mean has a maximum of one. The belongs_to side > means has exactly one. > > I don''t know (because I never tried it) what happens if you put > "belongs_to on both sides - i.e. make something exactly 1:1. Obviously > not relevant in this case, but I could see situations where for > performance reasons you may want that kind of partition - or when > wrapping an external database. > > CBelongs_to implies the presence of the foreign key so you can''t have it on both models (unless you let them be out-of-sync while being created). Has_one will only find one, but there could be many records that have the right foreign key. Just take any has_many association and change it to has_one (making the symbol singular, of course) and it will "just work" even though all the original records from the has_many are actually still there. Notice how I defined has_many :marriages and has_one :current_marriage that both use the same Marriage model. Those all belongs_to :wife (and belongs_to :husband, too) -Rob> On Apr 30, 1:08 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > wrote: >> On Apr 30, 2009, at 1:29 PM, serenobs wrote: >> >> >> >>> thanks for replies. >>> it gives me some thought about making active record association. >> >>> then what if the man is a single? >>> husband may not have current_marriage. >>> i was thinking that has_one means it should have one object. >>> But as you know not every man will marry. >> >>> please more advice for this novice. >> >> Then, man.current_marriage will be nil as will man.wife >> >> has_one really defines a 0/1 and has_many a 0/n relation. >> >> -Rob >> >> >> >> >> >>> On May 1, 2:17 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> >>> wrote: >>>> On Apr 30, 2009, at 12:56 PM, Commander Johnson wrote: >> >>>>> Did you consider >> >>>>> has_one :wife >> >>>>> And in Wife.rb >> >>>>> belongs_to :man >> >>>> Or: >>>> class Wife >>>> has_many :marriages >>>> has_one :current_marriage, :class_name => >>>> ''Marriage'', :conditions >>>> => { :current => true } >>>> has_one :husband, :through => :current_marriage >>>> end >> >>>> class Marriage >>>> belongs_to :wife >>>> belongs_to :husband >>>> end >> >>>> class Husband >>>> has_many :marriages >>>> has_one :current_marriage, :class_name => >>>> ''Marriage'', :conditions >>>> => { :current => true } >>>> has_one :wife, :through => :current_marriage >>>> end >> >>>> -Rob >> >>>> Rob Biedenharn http://agileconsultingllc.com >>>> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org >> >>>>> On Thu, Apr 30, 2009 at 6:49 PM, serenobs <seren...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>>> wrote: >> >>>>> (my post was deleted i don''t know why) >>>>> Hi. I have a question. >>>>> for example, when model records about man, suppose man can marry >>>>> with >>>>> 1 woman. >>>>> then i think code will be like this >>>>> class Man < ActiveRecord::Base >>>>> has_many: wife >>>>> end >> >>>>> Because one man can marry with up to 1 woman( 0 or 1 ) >>>>> it can''t be 1:1 relationship, didn''t it? >>>>> but at the same time more than 1 wife is not allowed. >>>>> then how can i model this relationship into rails code? >>>>> has_many :limit option is fit for this relation? >> >>>>> Thanks. >>>>> and i wish it is not deleted again. If it should be deleted please >>>>> let >>>>> me know why it should be.