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

Changeset 9068

Show
Ignore:
Timestamp:
03/21/08 18:21:56 (4 months ago)
Author:
rick
Message:

Allow association scoping for built/created records if :conditions is specified as a hash. Closes #11393 [miloops]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations.rb

    r9067 r9068  
    110110    # == Auto-generated methods 
    111111    # 
    112     # ===Singular associations (one-to-one) 
     112    # === Singular associations (one-to-one) 
    113113    #                                     |            |  belongs_to  | 
    114114    #   generated methods                 | belongs_to | :polymorphic | has_one 
     
    640640      #   if the real class name is +SpecialProduct+, you'll have to specify it with this option. 
    641641      # * <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>. 
    643645      # * <tt>:order</tt>       - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 
    644646      #   such as <tt>last_name, first_name DESC</tt> 
     
    982984      #   the +has_and_belongs_to_many+ association will use +project_id+ as the default association +foreign_key+. 
    983985      # * <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>. 
    985989      # * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment, 
    986990      #   such as <tt>last_name, first_name DESC</tt> 
  • trunk/activerecord/lib/active_record/associations/association_collection.rb

    r8957 r9068  
    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) } 
     
    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? 
  • trunk/activerecord/test/cases/associations_test.rb

    r9067 r9068  
    10761076  end 
    10771077 
     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 
    10781090  def test_dependent_delete_and_destroy_with_belongs_to 
    10791091    author_address = author_addresses(:david_address) 
     
    18881900  end 
    18891901 
     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 
    18901914  def test_uniq_after_the_fact 
    18911915    developers(:jamis).projects << projects(:active_record) 
  • trunk/activerecord/test/models/category.rb

    r8657 r9068  
    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...'