'ruby how properly update parent and child object
I have tree related models:
class Parent < ApplicationRecord
has_many :pets
has_one :kid
attr_accessor :notify_kid
after_save :let_the_kid_know
def let_the_kid_know
if notify_kid == '1'
self.kid.does_the_kid_know = true
self.kid.save
end
end
end
class Pet < ApplicationRecord
belongs_to :parent
end
class Kid < ApplicationRecord
belongs_to :parent
end
In my test I try to do something like this:
parent.notify_kid = '1'
parent.pets.first.name = 'Lollipop'
parent.save!
assert_equal true, parent.kid.does_the_kid_know
Test fails. Additionally, the pet item doesn't update either. In the test, I operate on objects already created in the database.
Solution 1:[1]
There is a syntax error here: def let_the_kid_know:, it should be def let_the_kid_know and another parent.notife_kid I guess is parent.notify_kid.
If that is not enough to fix you problem please post the error you see in the terminal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | javiyu |
