Hi All, in the api for has_many, under the :conditions option we see : Specify the conditions that the associated objects must meet in order to be included as a WHERE SQL fragment, such as price > 5 AND name LIKE ‘B%’. Record creations from the association are scoped if a hash is used. has_many<http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has_many>:posts, :conditions => {:published => true}will create published posts with @blog.posts.create or @blog.posts.build. however, in the api for has_one under the :conditions option we see : Specify the conditions that the associated object must meet in order to be included as a WHERE SQL fragment, such as rank = 5. So, we can''t automatically scope associated has_one records by conditions like we can has_many. seems to me that the behavior should be the same in both situations. I have a model with relationships like this class ThingContainer < AR::Base has_one :blue_thing, :class_name => "Thing", :conditions => {:color => "blue"} has_one :red_thing, :class_name => "Thing", :conditions => {:color => "red"} end class Thing < AR::Base belongs_to :thing_container end but when I call ThingContainer.build_blue_thing, it does not populate the color attribute. is this by design? or something that was simply left out? -C --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You appear to be trying to fit has_one into an implementation it wasn''t designed for. The has_one is supposed to be exactly that: ThingContainer has_one Thing, but you actually have ThingContainer has_many :things # well, really one of each of different kinds. There are likely several ways to handle this situation, but my instinct is to use named scopes for the color finds and validations to enforce the one-of-each. Another way would be to use STI to set up color models that inherit from Thing, with the has_ones from ThingContainer pointing to each. -eric On Oct 27, 1:47 pm, Chris Drappier <chris.drapp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > > in the api for has_many, under the :conditions option we see : > > Specify the conditions that the associated objects must meet in order to > be included as a WHERE SQL fragment, such as price > 5 AND name LIKE ‘B%’. > Record creations from the association are scoped if a hash is used. > has_many<http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&na...>:posts, > :conditions => {:published => true}will create published posts with > @blog.posts.create or @blog.posts.build. > > however, in the api for has_one under the :conditions option we see : > > Specify the conditions that the associated object must meet in order to > be included as a WHERE SQL fragment, such as rank = 5. > > So, we can''t automatically scope associated has_one records by conditions > like we can has_many. seems to me that the behavior should be the same in > both situations. > > I have a model with relationships like this > > class ThingContainer < AR::Base > has_one :blue_thing, :class_name => "Thing", :conditions => {:color => > "blue"} > has_one :red_thing, :class_name => "Thing", :conditions => {:color => > "red"} > end > > class Thing < AR::Base > belongs_to :thing_container > end > > but when I call ThingContainer.build_blue_thing, it does not populate the > color attribute. is this by design? or something that was simply left out? > > -C
This was just fixed on September 28: https://rails.lighthouseapp.com/projects/8994/tickets/3088 --Matt Jones On Oct 27, 4:47 pm, Chris Drappier <chris.drapp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > > in the api for has_many, under the :conditions option we see : > > Specify the conditions that the associated objects must meet in order to > be included as a WHERE SQL fragment, such as price > 5 AND name LIKE ‘B%’. > Record creations from the association are scoped if a hash is used. > has_many<http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&na...>:posts, > :conditions => {:published => true}will create published posts with > @blog.posts.create or @blog.posts.build. > > however, in the api for has_one under the :conditions option we see : > > Specify the conditions that the associated object must meet in order to > be included as a WHERE SQL fragment, such as rank = 5. > > So, we can''t automatically scope associated has_one records by conditions > like we can has_many. seems to me that the behavior should be the same in > both situations. > > I have a model with relationships like this > > class ThingContainer < AR::Base > has_one :blue_thing, :class_name => "Thing", :conditions => {:color => > "blue"} > has_one :red_thing, :class_name => "Thing", :conditions => {:color => > "red"} > end > > class Thing < AR::Base > belongs_to :thing_container > end > > but when I call ThingContainer.build_blue_thing, it does not populate the > color attribute. is this by design? or something that was simply left out? > > -C
so, will we see this patch applied in 2.3.5? On Tue, Oct 27, 2009 at 8:26 PM, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > This was just fixed on September 28: > > https://rails.lighthouseapp.com/projects/8994/tickets/3088 > > --Matt Jones > > On Oct 27, 4:47 pm, Chris Drappier <chris.drapp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi All, > > > > in the api for has_many, under the :conditions option we see : > > > > Specify the conditions that the associated objects must meet in order > to > > be included as a WHERE SQL fragment, such as price > 5 AND name LIKE > ‘B%’. > > Record creations from the association are scoped if a hash is used. > > has_many< > http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&na.. > .>:posts, > > :conditions => {:published => true}will create published posts with > > @blog.posts.create or @blog.posts.build. > > > > however, in the api for has_one under the :conditions option we see : > > > > Specify the conditions that the associated object must meet in order > to > > be included as a WHERE SQL fragment, such as rank = 5. > > > > So, we can''t automatically scope associated has_one records by conditions > > like we can has_many. seems to me that the behavior should be the same in > > both situations. > > > > I have a model with relationships like this > > > > class ThingContainer < AR::Base > > has_one :blue_thing, :class_name => "Thing", :conditions => {:color => > > "blue"} > > has_one :red_thing, :class_name => "Thing", :conditions => {:color => > > "red"} > > end > > > > class Thing < AR::Base > > belongs_to :thing_container > > end > > > > but when I call ThingContainer.build_blue_thing, it does not populate the > > color attribute. is this by design? or something that was simply left > out? > > > > -C > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---