-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Closed
Labels
Description
Running rails 4.0.0-beta1 on ruby 2.0.0-p1.
It seems as though when you perform code like the below, the where clauses are inherited by any callbacks it runs.
Category.where(full_name: 'root/bar').create
The above exhibits the problem. All of the below function as intended.
Category.new(full_name: 'root/bar').save
Category.create(full_name: 'root/bar')
Category.where(full_name: 'root/bar').new.save
By debugging on the rails console it reveals:
# This is basically running
Category.where(full_name: 'root/bar').where(full_name: 'root').first
# When this is expected
Category.where(full_name: 'root').first
This also applies to the first_or_create methods. I'm yet to test the find_or_create methods or the rails 3.x series. Should I write this into an active_record test? Any advice as to where to start?
I've created an example application with this problem here https://github.com/brocktimus/rails_create_differences. Example class used below.
class Category < ActiveRecord::Base
has_many :children, class_name: 'Category', foreign_key: :parent_id
belongs_to :parent, class_name: 'Category'
before_validation :assign_parents, on: :create
private
def assign_parents
parts = full_name.split '/'
self.name = parts.pop
self.parent = Category.where(full_name: parts.join('/')).first_or_initialize unless parts.empty?
end
end
adamcrown