ruby - Avoid Activerecord relation to be reloaded in Rails -


lats have relation:

@active_posts = post.where(active: true).limit(3) # returns 3 active posts, there more 3  

then modify of members of relation:

@active_posts.first.update active: false @active_posts.size # returns 3, need 2 remaining  

how can rest 2 active posts within relation? when try iterate through @active_posts, reloaded db , replace deactivated post active one. how can 2 active posts relation, remaining after deactivating third one?

use scope

class post < activerecord::base   scope :actived, -> { where(active: true) }   scope :deactived, -> { where(active: false) } end  @active_posts = post.actived # returns 3 active posts @active_posts.first.update active: false @active_posts.reload @active_posts = post.actived 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -