Search code examples
ruby-on-railsrubyruby-on-rails-4activerecordhas-many

Ruby On Rails : has_many collection not updated


I have a problem when i update an object in a has_many association :

l = self.posts.where(...)
l.each do |item|
  item.increment!(:nb, 2)         
  item.update
  item.save
end

When I do :

logger.info Post.all.inspect
logger.info self.posts.inspect

The posts are updated with the first line but not with the second

Is there a way to force the update or am i doing something wrong? I also tried to disable the cache but it still the same problem.


Solution

  • You can do

    logger.info self.posts(true).inspect
    

    By default, force_reload is false.