How do I return a scope that I think includes a select?
Hello,
I have a Model called Orgchart and it contains a position. In the users table there is an orgchart_id for each user. I have a select box on a form and I want to fill it with all the positions from orgcharts table that are NOT connected to a user in the users table. So essentally I feel like this should be a nil result from a nested select somehow but I'm drawing a blank. Probably the scope could be called available_positions as that would make more sense. Essentally return all of the Orgchart records where position is unused.
scope :empty_positions, -> { Not sure how to construct this! }
How do I do this?
Clint
I just tried to do the same with my Users and Roles
In my Role model I defined a class method call available
def self.available
@users = User.all
Role.where.not(id: @users.pluck(:role_id).uniq)
end
In my form for the dropdown I use this
<%= f.collection_select :role_id, Role.available, :id, :name, {class: 'form-control bootstrap-select', required: :required} %>
I think you could adjust the code above to work with your orgchart / positions
Thanks Amie! That worked!! So great to be able to get help to figure out some of the problems that crop up and where you have a mental block. So much saner than toiling with it for hours!
Clint