I''m not sure how to think about this experience with has_many association Suppose we have: class User < ActiveRecord::Base has_many :awesome_friends, :class_name => "Friend", :conditions => {:awesome => true} end (as well as the belongs_to in Friend class) And execute the code:>> my_user.awesome_friends << Friend.new(:name=>''jim'')Afterwards, when I inspect this friend object, I see that the user_id field is populated. But I would also expect to see the "awesome" column set to ''true'', which it is not. Furthermore, if I execute the following from the console:>> my_user.awesome_friends << Friend.new(:name=>''jim'') >> my_user.awesome_friends= [#<Friend id:1, name:"jim", awesome:nil>] # Quit and restart the console>> my_user.awesome_friends= [] Any thoughts on this? I suppose the conditions hash could be arbitrarily complex, making integration into the setter impossible. But in a way it feels like by default we are passing the condition ":user_id => self.id", and that gets set, so shouldn''t others? Thanks, Mike -- 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.
Hi Mike, The << operator calls concat on the association which adds an existing object without modifying it (except the foreign key). This is by design. If you use the create or build methods instead you should find that the conditions are set as you would expect provided that you continue to use hash style conditions. my_user.awesome_friends.create(:name => ''jim'') - Matt On Apr 26, 12:33 am, Mike Anderson <m...-HiqbC0dOLj3QT0dZR+AlfA@public.gmane.org> wrote:> I''m not sure how to think about this experience with has_many > association > > Suppose we have: > > class User < ActiveRecord::Base > has_many :awesome_friends, :class_name => "Friend", :conditions => > {:awesome => true} > end > > (as well as the belongs_to in Friend class) > > And execute the code: > > >> my_user.awesome_friends << Friend.new(:name=>''jim'') > > Afterwards, when I inspect this friend object, I see that the user_id > field is populated. But I would also expect to see the "awesome" > column set to ''true'', which it is not. > > Furthermore, if I execute the following from the console: > > >> my_user.awesome_friends << Friend.new(:name=>''jim'') > >> my_user.awesome_friends > > = [#<Friend id:1, name:"jim", awesome:nil>] > # Quit and restart the console>> my_user.awesome_friends > > = [] > > Any thoughts on this? I suppose the conditions hash could be > arbitrarily complex, making integration into the setter impossible. > But in a way it feels like by default we are passing the condition > ":user_id => self.id", and that gets set, so shouldn''t others? > > Thanks, Mike-- 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 Apr 26, 12:33 am, Mike Anderson <m...-HiqbC0dOLj3QT0dZR+AlfA@public.gmane.org> wrote:> I''m not sure how to think about this experience with has_many > association > > Suppose we have: > > class User < ActiveRecord::Base > has_many :awesome_friends, :class_name => "Friend", :conditions => > {:awesome => true} > end > > (as well as the belongs_to in Friend class) > > And execute the code: > > >> my_user.awesome_friends << Friend.new(:name=>''jim'') > > Afterwards, when I inspect this friend object, I see that the user_id > field is populated. But I would also expect to see the "awesome" > column set to ''true'', which it is not.If you''re building a new object, you can get this behavior by using build: new_friend = my_user.awesome_friends.build(:name => ''Jim'') new_friend will have awesome set to true. Note that this only works for associations with hash conditions; there''s not a reasonable way to turn something like: :conditions => ''some_field IS NOT NULL'' into a setter. --Matt Jones -- 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.