postgresql - Rails 4.2 and postgres - cannot update an attribute, possible reasons? -


i have stumbled upon strange behavior updates of record. have sample project in have models restaurant, hotel, restaurantrating, hotelrating...they indeticall, copy paste , different names in attributes. have rake task, use populate database. in rake task, creating ratings hotels , restaurants , in after_save callback, recounting average rating each attribute hotels , restaurants. same code works hotels, doesn't work restaurant since update rails 4.2.1 (it worked in 4.0.12).

the update looks this:

def update_restaurants_avg_rating   da::restaurant_rating_types.each |type|     result = activerecord::base.connection.execute("select avg(restaurant_ratings.#{type}) restaurant_ratings restaurant_ratings.restaurant_id = #{self.restaurant_id}").values[0][0].to_f     restaurant.where(id: self.restaurant_id).first.update_attribute("avg_rating_#{type}", result)   end end 

no update seen in log (i can see update command hotels not restaurants), value not persisted postgres. looks entirely different in play causes behavior. has idea, reason such behavior? btw have tested many possible commands, avoided usage of base.connection etc.

just completion, update code hotels:

def update_hotels_avg_rating   da::hotel_rating_types.each |type|     result = activerecord::base.connection.execute("select avg(hotel_ratings.#{type}) hotel_ratings hotel_ratings.hotel_id = #{self.hotel_id}").values[0][0].to_f     hotel.where(id: self.hotel_id).first.update_attribute("avg_rating_#{type}", result)         end end 

edit: have been digging deeper , found out, problem because when updating restaurant, attribute_names in active record empty, no column updated still...i don't know why. @ line: https://github.com/rails/rails/blob/d937a1175f10586b892842348c1d6ecaa47aad2e/activerecord/lib/active_record/persistence.rb#l528 attribute_names empty restaurant contains updated attribute + "updated_at" hotel.

but not sure part of rails responsible - guess part of rails not recognizing updated attribute.

so see update query, no change happens record?

check rollback -- if had guess i'd there validation failing.

perhaps try update_attributes!, throw exception if record invalid.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -