1

My rails app has a User model and a Role model. Each User belongs to one role and each role has many users. There three methods defined in the user model to check the role of that user def admin?, def user?, and def expert?.

The User class:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  validates_presence_of :name
  validates_presence_of   :avatar
  validates_integrity_of  :avatar
  validates_processing_of :avatar

  before_save :assign_role
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  belongs_to :role
  has_many :items
  belongs_to :organization
  has_many :expertkeywordmodels
  has_many :keywords, through: :expertkeywordmodels

  def assign_role
    self.role = Role.find_by name: "Admin" if self.role.nil?
  end

  def self.with_role(role)
     my_role = Role.find_by_name(role)
     where(:role => my_role)
  end

  def admin?
    self.role.name == "Admin"
  end

  def user?
    self.role.name == "User"
  end

  def expert?
    self.role.name == "Expert"
  end

end

The Role class:

class Role < ActiveRecord::Base
  has_many :users
end

I am trying to create a collection_select only with users that have expert role. Something like:

<%= collection_select(:keyword, :user_ids, User.where('expert?'), :id, :name, {prompt: true}, {:multiple => true}) %>

But it does not recognize expert? as a method. I was wondering if anyone knows how can I perform this query. I am sorry if this is a naive question as I am new to rails.

Thanks, Amir

4
  • what happens when you try User.where(role: {name: "expert"}) Commented Jun 23, 2016 at 17:06
  • If you fire up rails c and try this User.last.expert? what do you get? Commented Jun 23, 2016 at 17:06
  • User.last.expert? returns false and User.last.admin? returns true which means it is correct. @KarimTarek Commented Jun 25, 2016 at 0:24
  • @ruby_newbie it returns there is no such column role.name Commented Jun 25, 2016 at 0:25

2 Answers 2

1

User.where('expert?') doesn't really makes sense for the database, because it would translate to SQL like:

SELECT * FROM users WHERE expert?;

And obviously expert? isn't a valid SQL expression. expert? is only available in the context of your code.

Instead you need to write that logic in a way that translates to valid SQL and makes sense in the context of your database schema. My guess is that the following might work:

User.joins(:role).where(roles: { name: 'Expert'})

You might want to define a scope in your User model, like this:

scope :experts, -> { joins(:role).where(roles: { name: 'Expert'}) }

Than User.experts would return all users that have the expert role.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It worked like a charm and definitely I need to improve my SQL knowledge.
0

Not for nothing, but you have three methods in your user model that all set the same field, just differently.

def role(role_type)
  self.role.name = role_type
end

To get your desired require to work properly - you can either write a scope or a method.

def get_roles(role_type)
   User.role.name = role_type
end

Rail Guides are always extremely helpful. http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.