ruby on rails - How to exclude a collection of records with ActiveRecord? -


i have following:

= select :client, :id, user.all.map {|u| [u.username, u.id]}, include_blank: "add client username" 

i'd exclude records user.all match current_user.manager_users. point select box doesn't show users added manager_users array has_many collection.

you that:

= select :client, :id, user.where("users.id not in (?)", current_user.manager_users.pluck(:client_id)).map {|u| [u.username, u.id]}, include_blank: "add client username" 

the new stuff here:

user.where("users.id not in (?)", current_user.manager_users.pluck(:client_id))     .map{ |u| [u.username, u.id] } 

the current_user.manager_users.pluck(:client_id) part retrieve (only on db-level) client_ids of manager_users linked current_user.

hope helps!


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -