Forum: Ruby on Rails Using collection<< on has_many with :conditions.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
407016c9d4109e726fc30681b21324cc?d=identicon&s=25 Jarl Friis (jarl)
on 2009-04-20 12:09
(Received via mailing list)
Hi.

I have a has_many relation with a hashed condition, taking from the RI
doc:

:conditions
           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 :posts,
           :conditions => {:published => true} will create published
           posts with @blog.posts.create or @blog.posts.build.

I wonder why the behaviour of @blog.posts<< is not also modified when
using hashed conditions. The thing is when I have :conditions =>
{:published => true}

and issue @blog.posts.create(:published => false) will result in a new
(published) post, i.e. it will override the :published attributes,
the same holds for @blog.posts.build(:published => false), but when I
do a @blog.posts << Post.new(:published => false), it will not be
overruled by my condition, it will not even fail my condition. It will
simply end up in the database outside the scope of the condition.

Is this a design decision (of the has_many :condition attribute)? or
is this actually a bug/feature request?

Jarl
This topic is locked and can not be replied to.