ruby on rails - how to make ActiveRecord bump updated_at on new has_many association -


when adding records has_many association , saving parent, parent's updated_at not bumped:

order = order.new order.save pp order   => #<order id: 3, created_at: "2013-04-18 15:25:09", updated_at: "2013-04-18 15:25:09">  order.line_items << lineitem.new() pp order.line_items   => [#<lineitem id: 4, order_id: 3, created_at: "2013-04-18 15:26:16", updated_at: "2013-04-18 15:26:29", product_id: 123>]  order.save pp order.reload   => #<order id: 3, created_at: "2013-04-18 15:25:09", updated_at: "2013-04-18 15:25:09"> 

this makes sense, beacause order not touched; foreign key lives on lineitem.

however, in case, i'd query order , find orders have recieved new lineitems in last 30 minutes (i.e: "updated" in last 30 minutes).

i not sure best here, , how achieve easily. there flag or method set have ar update parents updated_at described? or should via hook? if so, hook? or should store timestamp in different column?

note query line_items.updated_at trough join(:line_items), make code more cluttered , less performant: or preferred way such issues in rails?

you need include touch: true along belongs_to :order association.

class order < activerecord::base   has_many :line_items end   class lineitem < activerecord::base   belongs_to :order, touch: true end 

this update updated_at column associated order. more docs here


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

node.js - Bad Request - node js ajax post -

uitableview - Create and use custom prototype table cells in xamarin ios using storyboard -