Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11393: associations_respects_hash_conditions.diff

File associations_respects_hash_conditions.diff, 3.1 kB (added by miloops, 2 years ago)
  • test/models/category.rb

    old new  
    99                          :association_foreign_key => 'post_id', 
    1010                          :select => 'posts.*, 1 as correctness_marker') 
    1111 
     12  has_and_belongs_to_many :post_with_conditions, 
     13                          :class_name => 'Post', 
     14                          :conditions => { :title => 'Yet Another Testing Title' } 
    1215  def self.what_are_you 
    1316    'a category...' 
    1417  end 
  • test/cases/associations_test.rb

    old new  
    10141014    assert_equal 1, Client.find_all_by_client_of(firm.id).size 
    10151015  end 
    10161016 
     1017  def test_creation_respects_hash_condition 
     1018    ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build 
     1019     
     1020    assert        ms_client.save 
     1021    assert_equal  'Microsoft', ms_client.name 
     1022     
     1023    another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create 
     1024 
     1025    assert        !another_ms_client.new_record? 
     1026    assert_equal  'Microsoft', another_ms_client.name 
     1027  end 
     1028 
    10171029  def test_dependent_delete_and_destroy_with_belongs_to 
    10181030    author_address = author_addresses(:david_address) 
    10191031    assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id] 
     
    18261838    assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated 
    18271839  end 
    18281840 
     1841  def test_creation_respects_hash_condition 
     1842    post = categories(:general).post_with_conditions.build(:body => '') 
     1843     
     1844    assert        post.save 
     1845    assert_equal  'Yet Another Testing Title', post.title 
     1846     
     1847    another_post = categories(:general).post_with_conditions.create(:body => '') 
     1848 
     1849    assert        !another_post.new_record? 
     1850    assert_equal  'Yet Another Testing Title', another_post.title 
     1851  end 
     1852 
    18291853  def test_uniq_after_the_fact 
    18301854    developers(:jamis).projects << projects(:active_record) 
    18311855    developers(:jamis).projects << projects(:active_record) 
  • lib/active_record/associations/association_collection.rb

    old new  
    202202      private 
    203203 
    204204        def create_record(attrs) 
     205          attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) 
    205206          ensure_owner_is_not_new 
    206207          record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) } 
    207208          if block_given? 
     
    212213        end 
    213214 
    214215        def build_record(attrs) 
     216          attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) 
    215217          record = @reflection.klass.new(attrs) 
    216218          if block_given? 
    217219            add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }