This project is archived and is in readonly mode.

#6252 ✓resolved
Jan Xie

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

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>

Referenced by

Pages