Ruby On Rails : has_many collection not updated

327 views Asked by At

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.

1

There are 1 answers

1
Arup Rakshit On BEST ANSWER

You can do

logger.info self.posts(true).inspect

By default, force_reload is false.