My Rails model has some bad lines. In general, first error is when accessing roles
variable from has_access
def. And then, second is when making where
action.
class Organization < ActiveRecord::Base
has_and_belongs_to_many :organizations_users
belongs_to :user
validates :name, :presence => true, :length => { minimum: 4, maximum: 35 }, uniqueness: true
roles = { :administrator => 1, :support => 2 }
def has_access(chosen_user, role)
self.organizations_users.where('user_id = ? and role = ?', chosen_user.id, roles[role]).exists?
end
def add_user(chosen_user, role)
if !self.has_access(chosen_user, role)
self.organizations_users.create({user_id: chosen_user.id, role: roles[role]})
end
has_access(chosen_user,role)
end
end
I need also to query over organizations_users
table to get information about access. How can I fix that?