This project is archived and is in readonly mode.

incorrect scope in associated collection methods
Reported by Jan Xie | January 5th, 2011 @ 06:46 AM
Just found a recent change may lead to incorrect semantic: https://github.com/rails/rails/commit/31d101879f1acae604d24d831a4b8...
It's hard to describe it so I attached a test case .. They fail in edge rails but will pass if you revert to a commit prior to 31d101879f1acae604d24d831a4b82a4482acf31.
For those who don't want to read the attachment:
Suppose you a model, Brand, acts as a tree:
class Brand < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Brand'
  has_many :children, :class_name => 'Brand', :foreign_key => :parent_id
  before_validation :find_parent_and_do_something
  private
    def find_parent_and_do_something
      if self.parent_id
        Brand.find(parent_id) # and do sth. # break line
      end
    end
end
Then code like
b = Brand.create!
b.children.create! # break line
will raise a RecordNotFound excpetion on the line "break line", because Brand has incorrect scope (:parent_id => 123) in associate collection #create.
Comments and changes to this ticket
- 
            
         Jan Xie January 5th, 2011 @ 06:51 AM- no changes were found...
 
- 
         Jon Leighton January 7th, 2011 @ 08:05 PM- State changed from new to open
- Assigned user set to Jon Leighton
- Importance changed from  to Low
 Hi, Thanks for the report. This is on my list to look at. Jon 
- 
            
         hemant January 19th, 2011 @ 10:08 PMHi Jon, I could replicate the issue using the tests provided by Jan Xie. In association_collection.rb I've removed scoped.scoping around build_record(attrs, &block) as 
 the scopes are already being injected in following method populate_with_current_scope_attributes present in base.rbdef populate_with_current_scope_attributes if scope = self.class.send(:current_scoped_methods) create_with = scope.scope_for_create create_with.each { |att,value| respond_to?("#{att}=") && send("#{att}=", value) } end endThis change fix all the tests though but I might be missing/overlooking something crucial here by removing scoping. 
 Please comment.Thanks, 
 Hemant
- 
         Jon Leighton January 19th, 2011 @ 10:12 PMHi hemant, Thanks for the comment. I haven't had a chance to look into this properly yet but I haven't forgotten and I promise I will get to it :) Cheers, 
 Jon
- 
         Repository January 30th, 2011 @ 08:32 PM- State changed from open to resolved
 (from [63c73dd0214188dc91442db538e141e30ec3b1b9]) We shouldn't be using scoped.scoping { ... } to build associated records, as this can affect validations/callbacks/etc inside the record itself [#6252 state:resolved] https://github.com/rails/rails/commit/63c73dd0214188dc91442db538e14... 
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 
         6252 
          incorrect scope in associated collection methods
        (from [63c73dd0214188dc91442db538e141e30ec3b1b9])
We shou... 6252 
          incorrect scope in associated collection methods
        (from [63c73dd0214188dc91442db538e141e30ec3b1b9])
We shou...
 hemant
      hemant
 Jan Xie
      Jan Xie
 Jeremy Kemper
      Jeremy Kemper
 Jon Leighton
      Jon Leighton
 Piotr Sarnacki
      Piotr Sarnacki