It seems that I found a bug. I have two similar models that behave differently.
I have a Post
model that belongs_to an Author
.
I have a Task
model that is self-referencing.
Model code:
app/models/author.rb:
class Author < ActiveRecord::Base
has_many :posts
end
app/models/post.rb:
class Post < ActiveRecord::Base
belongs_to :author
scope :published, -> { where(status: 1) }
after_create do
puts '========================================='
puts "author_id is present: '#{author_id}'"
puts "what about task association? '#{author}'"
puts '========================================='
end
end
app/models/task.rb:
class Task < ActiveRecord::Base
belongs_to :task
has_many :tasks
scope :published, -> { where(status: 1) }
after_create do
puts '========================================='
puts "task_id is present: '#{task_id}'"
puts "what about task association? '#{task}'"
puts '========================================='
end
end
Both Post
and Task
are similarly scoped, but behave differently:
Author.create.posts.published.create # works
Task.create.tasks.published.create # doesn't work
Task.create.tasks.create # works
There is an after_create
callback in Task model, where it should print the parent Task
but it is nil despite task_id
having the correct ID of parent.
Why is it behaving differently?