ruby on rails - How to get the highest value from a child association -


i have order model has_many items.

app/models/order.rb

class order < activerecord::base   has_many :items   before_save :set_status    enum item_status: [:one, :two, :three]    private     def set_status       self.items.each |i|         self.item_status = i.item_status if i.item_staus > self.item_status       end     end end 

both models have same "item_status" enum , attribute.

i think comparison not working because not comparing actual int value rather enum string value.

how can fix this?

you should use maximum perform query in database, using stored integer value rather activerecord-wrapped records you're performing comparisons agains symbols:

def set_status   self.item_status = self.items.maximum(:item_status) end 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -