Changeset 9068
- Timestamp:
- 03/21/08 18:21:56 (4 months ago)
- Files:
-
- trunk/activerecord/lib/active_record/associations.rb (modified) (3 diffs)
- trunk/activerecord/lib/active_record/associations/association_collection.rb (modified) (2 diffs)
- trunk/activerecord/test/cases/associations_test.rb (modified) (2 diffs)
- trunk/activerecord/test/models/category.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/associations.rb
r9067 r9068 110 110 # == Auto-generated methods 111 111 # 112 # === Singular associations (one-to-one)112 # === Singular associations (one-to-one) 113 113 # | | belongs_to | 114 114 # generated methods | belongs_to | :polymorphic | has_one … … 640 640 # if the real class name is +SpecialProduct+, you'll have to specify it with this option. 641 641 # * <tt>:conditions</tt> - specify the conditions that the associated objects must meet in order to be included as a +WHERE+ 642 # SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>. 642 # SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>. Record creations from the association are scoped if a hash 643 # is used. <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt> 644 # or <tt>@blog.posts.build</tt>. 643 645 # * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 644 646 # such as <tt>last_name, first_name DESC</tt> … … 982 984 # the +has_and_belongs_to_many+ association will use +project_id+ as the default association +foreign_key+. 983 985 # * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a +WHERE+ 984 # SQL fragment, such as <tt>authorized = 1</tt>. 986 # SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are scoped if a hash is used. 987 # <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt> 988 # or <tt>@blog.posts.build</tt>. 985 989 # * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 986 990 # such as <tt>last_name, first_name DESC</tt> trunk/activerecord/lib/active_record/associations/association_collection.rb
r8957 r9068 203 203 204 204 def create_record(attrs) 205 attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) 205 206 ensure_owner_is_not_new 206 207 record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) } … … 213 214 214 215 def build_record(attrs) 216 attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) 215 217 record = @reflection.klass.new(attrs) 216 218 if block_given? trunk/activerecord/test/cases/associations_test.rb
r9067 r9068 1076 1076 end 1077 1077 1078 def test_creation_respects_hash_condition 1079 ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build 1080 1081 assert ms_client.save 1082 assert_equal 'Microsoft', ms_client.name 1083 1084 another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create 1085 1086 assert !another_ms_client.new_record? 1087 assert_equal 'Microsoft', another_ms_client.name 1088 end 1089 1078 1090 def test_dependent_delete_and_destroy_with_belongs_to 1079 1091 author_address = author_addresses(:david_address) … … 1888 1900 end 1889 1901 1902 def test_creation_respects_hash_condition 1903 post = categories(:general).post_with_conditions.build(:body => '') 1904 1905 assert post.save 1906 assert_equal 'Yet Another Testing Title', post.title 1907 1908 another_post = categories(:general).post_with_conditions.create(:body => '') 1909 1910 assert !another_post.new_record? 1911 assert_equal 'Yet Another Testing Title', another_post.title 1912 end 1913 1890 1914 def test_uniq_after_the_fact 1891 1915 developers(:jamis).projects << projects(:active_record) trunk/activerecord/test/models/category.rb
r8657 r9068 10 10 :select => 'posts.*, 1 as correctness_marker') 11 11 12 has_and_belongs_to_many :post_with_conditions, 13 :class_name => 'Post', 14 :conditions => { :title => 'Yet Another Testing Title' } 12 15 def self.what_are_you 13 16 'a category...'