$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
$ rails -v
Rails 4.2.0
$ grep DESCR /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
I'm trying to build and expose a subset of records to my view(s) from my controller like this:
@assigned_roles = @user.user_roles.select{|x| x.is_required }.map{ |ur| ur.role }
if @assigned_roles.blank?
@optional_roles = Role.all
else
@optional_roles = Role.where('id NOT IN (?)',@assigned_roles.map{ |r| r.id})
end
It works but I'm looking for something more elegant. the .where NOT IN doesn't work as I expect when @assigned_roles
is empty.
I have models for Role and User which have many
of eachother through user_roles
which bears the t.boolean "is_required", default: true, null: false
attribute.
Sorry I'm a total n00b so I don't know what else to include. Much Thanks!