aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-03-21 18:21:56 +0000
committerRick Olson <technoweenie@gmail.com>2008-03-21 18:21:56 +0000
commitc23c9bd11bc16618a1765eb61424014912a6e1d7 (patch)
tree4fcea83532d0c64e4aa86f455a5cabecb74c851a /activerecord/lib
parent273b21faa911681ed4b6c748676146e0f6eed0a0 (diff)
downloadrails-c23c9bd11bc16618a1765eb61424014912a6e1d7.tar.gz
rails-c23c9bd11bc16618a1765eb61424014912a6e1d7.tar.bz2
rails-c23c9bd11bc16618a1765eb61424014912a6e1d7.zip
Allow association scoping for built/created records if :conditions is specified as a hash. Closes #11393 [miloops]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9068 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb2
2 files changed, 9 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 0e07ee4913..aa68a42c44 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -109,7 +109,7 @@ module ActiveRecord
#
# == Auto-generated methods
#
- # ===Singular associations (one-to-one)
+ # === Singular associations (one-to-one)
# | | belongs_to |
# generated methods | belongs_to | :polymorphic | has_one
# ----------------------------------+------------+--------------+---------
@@ -639,7 +639,9 @@ module ActiveRecord
# from the association name. So <tt>has_many :products</tt> will by default be linked to the +Product+ class, but
# if the real class name is +SpecialProduct+, you'll have to specify it with this option.
# * <tt>:conditions</tt> - specify the conditions that the associated objects must meet in order to be included as a +WHERE+
- # SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>.
+ # SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>. Record creations from the association are scoped if a hash
+ # is used. <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
+ # or <tt>@blog.posts.build</tt>.
# * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
# such as <tt>last_name, first_name DESC</tt>
# * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
@@ -981,7 +983,9 @@ module ActiveRecord
# guessed to be the name of the associated class in lower-case and +_id+ suffixed. So if the associated class is +Project+,
# the +has_and_belongs_to_many+ association will use +project_id+ as the default association +foreign_key+.
# * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a +WHERE+
- # SQL fragment, such as <tt>authorized = 1</tt>.
+ # SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are scoped if a hash is used.
+ # <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
+ # or <tt>@blog.posts.build</tt>.
# * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
# such as <tt>last_name, first_name DESC</tt>
# * <tt>:uniq</tt> - if set to +true+, duplicate associated objects will be ignored by accessors and query methods
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 28c257a7e4..beebd72bba 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -202,6 +202,7 @@ module ActiveRecord
private
def create_record(attrs)
+ attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
ensure_owner_is_not_new
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) }
if block_given?
@@ -212,6 +213,7 @@ module ActiveRecord
end
def build_record(attrs)
+ attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
record = @reflection.klass.new(attrs)
if block_given?
add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }