ruby on rails - How top pop last element of ActiveRelation? -


say have query:

red_houses = user.houses.where(color: 'red') 

how can pop last element of red_houses?

i know can red_houses[1..-2], not red_houses.pop, there way missing?

basically want delete last element , remaining relation without last element on it.

you have 2 methods remove records association: delete , destroy. both of them can called object want remove or id. (also can use list of object or list of ids parameters).

since :delete works according strategy specified :dependent option (with default of nullify), :destroy remove records database, ignoring :dependent option.

but, if don't want persist @ db action. before remove, need working variable (calling :to_a), , can call pop:

red_houses = user.houses.where(color: 'red').to_a red_houses.pop 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -