I have this method in my book model but now I realize I need this in a category model as well:
def proper_user?(logged_in_user)
return false unless logged_in_user.is_a? User
user == logged_in_user
end
I now have this method duplicated in the books model and the category model. Both category and books has belongs_to :user and both have the user_id:integer in the table as well. I simply want to extract this somewhere where so I can its DRY.
i tried to put the method in application_controller.rb but it says undefined method `proper_user?’ for #
Thanks
Jeff
I think you’d want to be able to call this method like this:
book.proper_user?(current_user)So it would work best to define it in each model rather then in User. This is best done by mixing in a module with the method:
and including it in each model:
The module can go in a source file in config/initializers, or you can put it elsewhere and change
config.autoload_pathsin config/environment.rb to point to the location.