In Ruby/Rails, how to get current value of column of queried model in a different model class? -


i querying model within method in different model's class. how can refer queried model in sort of "rails way" preferably? please see code understand:

modela.where("id = ?", self.model_a_id).first.update_column(:attrbute_a, ???.attribute_a + self.attribute_b ) 

the "???" denotes how refer attribute model a. using self refer model b, class inside of.

you can way:

model = modela.find(model_a_id) model.update_column(:attribute_a, model.attribute_a + attribute_b) 

btw, why don't use associations?


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -