andrenth-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-May-21 14:49 UTC
Help with relations
Hello I have the three database models: "seller", "client" and "account". A seller may have many clients and many accounts, but I would like to add the constraint that given a seller and a client, only one account may exist between them. Is there a way to express this contraint using ActiveRecord relations? Thanks in advance, Andre -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
andrenth-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hello > > I have the three database models: "seller", "client" and "account". A > seller may have many clients and many accounts, but I would like to > add the constraint that given a seller and a client, only one account > may exist between them. > > Is there a way to express this contraint using ActiveRecord relations?Yes -- validates_uniqueness_of will do the trick -- but never rely solely on ActiveRecord constraints. You need a unique index (and foreign key constraints) on the DB side too.> > Thanks in advance, > AndreBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
andrenth-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-May-21 16:41 UTC
Re: Help with relations
On May 21, 12:20 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Yes -- validates_uniqueness_of will do the trick -- but never rely > solely on ActiveRecord constraints. You need a unique index (and > foreign key constraints) on the DB side too.I am already using the validations and key constrains. I was actually wondering about the possibility of having Active Record generate helper methods reflecting that kind of relation. Thanks, Andre -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 May 2010 15:49, andrenth-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andre.nathan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > > I have the three database models: "seller", "client" and "account". A > seller may have many clients and many accounts, but I would like to > add the constraint that given a seller and a client, only one account > may exist between them. > > Is there a way to express this contraint using ActiveRecord relations?Does Client belongs_to seller, and Account belongs_to seller? If so then you could put client and account together in one table. Alternatively have Client has_one account and Account belongs_to client, or the other way round. This doesn''t work if you have HABTM relationship of course. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.