My parent class sometimes does not load all its children in the after_save child.
I have two models:
class Parent < ActiveRecord::Base has_many :children def update_something # explained below end end class Child < ActiveRecord::Base belongs_to :parent after_save :tell_parent_to_update def tell_parent_to_update parent.update_something end end
I have a test I'm working on, which only checks 2 things. parent.children.count and parent.children.length . Both should be 4. I understand that the count is sometimes different, but (as far as I know) it should not be here.
If I define update_something to just update_something over children :
def update_something children.each do |child| end end
the test fails - the loop will be executed once (and will return an array of one child - the first child created).
Otherwise, I can put any code if it does not mention children , and it will work. This is like a challenge for children forcing the association to download the wrong thing.
Forced reboot fixes it:
def update_something children(true).each do |child| end end
but these are hackies, and I would rather fix the root problem if possible.
Is this my mistake or the mistake of the rails (and if so, can I do this to get around this?)
I doubt this is important, but it is a test environment using sqlite3. Although it will also fail in the dev environment if I create and test entries in the same developer console session.
ruby ruby-on-rails ruby-on-rails-4
user4816935
source share