I have a user model which has association with groups. The relation is given below
class User < ActiveRecord:;Base
has_many :groups
end
class Group < ActiveRecord::Base
belongs_to :user
scope :user_groups, lambda { where(arel_table(:user_id).eq(@current_user.id) }
end
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find(session['entity.user']) if session['entity.user']
end
end
I want to filter current_user groups in my select_tag in group index page.
How to achieve this?
You should not access to this data inside the model (it is violating the MVC). You would be better to pass it as an argument to the scope:
And call it like this:
Note: You can pass either an id (Integer) or a User Object to the
user_groupslambda.