Aliasing Methods in Rails 3
I'm using Devise for authentication, but I'm using two user models, Buyer and Seller.
By default, Devise will开发者_开发问答 generate two methods such as current_buyer and current_seller.
I want to clean up my code and in a before_filter in my ApplicationController, I would like to alias the appropriate method to current_user.
What's the best way to do this in Rails 3?
I agree with balexand that you should have one model for login and separate them by roles.
But it's up to you and there is the way to do it by using method:
def current_user
current_seller || current_buyer
end
This would return seller
, buyer
or nil
.
You might be making your design more complicated than necessary by having two user models. You may want to consider using one User model for authentication. Then you can have roles for Buyer and Seller if needed.
But if you really just want to alias a method in ApplicationController, you can just do this:
private
alias :current_user :current_seller
Or if you need something more dynamic you can just use a method.
精彩评论