Reputation: 94
I have the table "tools" and "lend".
Im using Rails3 and when i create a lend i would like it change the attribute status of the tool to 'U'.
Would this is possible?
i tried on the model lend
after_save :change_status
def change_status
tools.update_attribute(status, 'U')
end
i tried too, on the same model:
after_save :change_status
def change_status
self.tool.update_attribute(status, 'U')
end
No success or warning on debug log.
Sugestions?
Thanks! :)
Upvotes: 0
Views: 120
Reputation: 2396
Firstly, I assume that your Lend model has_many :tools
In order to be able to do something like tool.update_attribute
you'll need to work with the accepts_nested_attributes_for
Take a look at these links and they will probably set you on the right path:
RailsCasts #196 Nested Model Form Part 1
Active Record Nested Attributes
Hope this helps.
Upvotes: 0
Reputation: 1251
What is the relationship between lend and tool? If Lend has_many tools, you will have to do something like this:
def change_status
tools.each { |tool| tool.update_attributes(status: 'U') }
end
Note also that I am using update_attributes because update_attribute (singular) will be deprecated soon.
BTW, you should create a method in Tool to update the attribute, the Lend model should not be aware about how to set a tool as loaned. Something like
def loaned!
update_attributes status: 'U'
end
Upvotes: 0