I need some advice.
In my controller, I have an error: "The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined ..."
Group controller code:
@groups = @q.result.order('name').accessible_by(current_ability,:read).page(params[:page]).per(20)
In ability.rb file:
can :read, Group do |group|
group.try(:group_type) == 1 || group.try(:user) == user || user_just_joined(user, group.id)
end
user_just_joined is a function, that return true or false, if user is related in this group, or not.
Yes, i know that I should prepare code in this way, to avoid this error:
can :read, Group, :group_type => 1
can :read, Group, :user => user
can :read, Group, user_just_joined(user, :id)
// above code shoult create "OR" statement, analogical to first example
But: can :read, Group, user_just_joined(user, :id)
doesn't work, :id value is undefined.
What is the best solution to using an "OR" statement with function, that return true or false dependent on passed params?
From the documentation, this might help... the accessible_by
is a class method, and therefore like the index action in this case.
Fetching Records
A block's conditions are only executable through Ruby. If you are Fetching Records using accessible_by it will raise an exception. To fetch records from the database you need to supply an SQL string representing the condition. The SQL will go in the WHERE clause, if you need to do joins consider using sub-queries or scopes (below).
can :update, Project, ["priority < ?", 3] do |project|
project.priority < 3
end
If you are using load_resource and don't supply this SQL argument, the instance variable will not be set for the index action since they cannot be translated to a database query.