Reputation: 2917
I have a method in my model,
def self.interests(user)
City.joins("INNER JOIN property_of_interests ON property_of_interests.city_id = cities.id").where(:property_of_interests => {:user_id => user})
end
I want to call this method into my controller before index and new action, how can I can I do that ?
And all the model and controller are of different entities
If I use before_filter in the controller how will I pass current_user argument to the method ?
Upvotes: 1
Views: 977
Reputation: 3143
You can pass a lambda, in this case {@interests = Widget.interests current_user}
, into before_action
to achieve what you want.
app/controllers/widgets_controller.rb
class WidgetsController < ApplicationController
before_action(only: [:show]) {@interests = Widget.interests current_user}
def show
# do something with @interests
end
end
app/models/widget.rb
class Widget < ActiveRecord::Base
def self.interests(user)
City.joins("INNER JOIN property_of_interests ON property_of_interests.city_id = cities.id").where(:property_of_interests => {:user_id => user})
end
end
Upvotes: 4