class Foo < ActiveRecoed::Base has_many :www_links, :as => :owner end class WwwLink < ActiveRecord::Base belongs_to :owner, :polymorphic => true attr_accessible :href end I want to ensure that each Foo instance does not have same links (href). My first attempt would be to write validation in Foo that checks all its www_links. But then I would also have to do it in WwwLink class. That would not be DRY. Any other ideas? -- 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.
On 2 January 2011 13:42, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> class Foo < ActiveRecoed::Base > has_many :www_links, :as => :owner > end > > class WwwLink < ActiveRecord::Base > belongs_to :owner, :polymorphic => true > attr_accessible :href > end > > I want to ensure that each Foo instance does not have same links (href).I don''t understand what you mean by the above. Colin> > My first attempt would be to write validation in Foo that checks all its > www_links. But then I would also have to do it in WwwLink class. That > would not be DRY.-- 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.
Colin Law wrote in post #971843:> On 2 January 2011 13:42, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> class Foo < ActiveRecoed::Base >> has_many :www_links, :as => :owner >> end >> >> class WwwLink < ActiveRecord::Base >> belongs_to :owner, :polymorphic => true >> attr_accessible :href >> end >> >> I want to ensure that each Foo instance does not have same links (href). > > I don''t understand what you mean by the above. > > Colintest "should not allow for adding two same links" do # given foo1 = create_foo.save! link1 = foo1.www_links.create(:href => ''wp.pl'') link1.save! foo2 = create_foo.save! link2 = foo2.www_links.create(:href => ''wp.pl'') # when link2.save! # then errors occured ! end I will put this logic in WwwLinks controller. -- 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.
On 2 January 2011 14:11, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #971843: >> On 2 January 2011 13:42, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> class Foo < ActiveRecoed::Base >>> has_many :www_links, :as => :owner >>> end >>> >>> class WwwLink < ActiveRecord::Base >>> belongs_to :owner, :polymorphic => true >>> attr_accessible :href >>> end >>> >>> I want to ensure that each Foo instance does not have same links (href). >> >> I don''t understand what you mean by the above. >> >> Colin > > test "should not allow for adding two same links" do > # given > foo1 = create_foo.save! > link1 = foo1.www_links.create(:href => ''wp.pl'') > link1.save! > > foo2 = create_foo.save! > link2 = foo2.www_links.create(:href => ''wp.pl'') > > # when > link2.save! > > # then > errors occured ! > endSo you are just wanting uniqueness of www_link.href? Will not validates_uniqueness_of in WwwLink class (backed up by a unique constraint in the database of course) do the job? I think in this case it will be the foo2.www_links.create that will fail. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #971847:> On 2 January 2011 14:11, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> >> link1.save! >> >> foo2 = create_foo.save! >> link2 = foo2.www_links.create(:href => ''wp.pl'') >> >> # when >> link2.save! >> >> # then >> errors occured ! >> end > > So you are just wanting uniqueness of www_link.href? Will not > validates_uniqueness_of in WwwLink class (backed up by a unique > constraint in the database of course) do the job? I think in this > case it will be the foo2.www_links.create that will fail. > > ColinThe constraint is on "owner" and "href", not "href" itself. I can have multiple links with the same href. But those cannot be with the same owner_id. -- 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.
>> So you are just wanting uniqueness of www_link.href? Will not >> validates_uniqueness_of in WwwLink class (backed up by a unique >> constraint in the database of course) do the job? I think in this >> case it will be the foo2.www_links.create that will fail. >> Colin > The constraint is on "owner" and "href", not "href" itself. > I can have multiple links with the same href. But those cannot be with > the same owner_id.validates_uniqueness_of(:href, :scope => [:owner_type, :owner_id]) -- 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.
On 2 January 2011 14:34, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #971847: >> On 2 January 2011 14:11, Michal Burak <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>>> >>> link1.save! >>> >>> foo2 = create_foo.save! >>> link2 = foo2.www_links.create(:href => ''wp.pl'') >>> >>> # when >>> link2.save! >>> >>> # then >>> errors occured ! >>> end >> >> So you are just wanting uniqueness of www_link.href? Will not >> validates_uniqueness_of in WwwLink class (backed up by a unique >> constraint in the database of course) do the job? I think in this >> case it will be the foo2.www_links.create that will fail. >> >> Colin > > > The constraint is on "owner" and "href", not "href" itself. > I can have multiple links with the same href. But those cannot be with > the same owner_id.That is not what your test says, you have too Foo objects, foo1 and foo2 and are asking it to fail when you add the link to the second one. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.