You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
def initialize(user)
user ||= login_as_trial_user
private
def login_as_trial_user
name = "anonymous_#{Time.now.to_i + rand}"
if User.find_by_username(name)
UserSession.create(User.find_by_username(name),true)
else
guest_role = User.create(:username => name, :password => name, :password_confirmation => name, :role => "guest", :email => "change@this.com")
UserSession.create(guest_role, true)
end
@current_user_session = UserSession.find
guest_role
end
This is in the Ability.rb
Is that the correct way to go? Or should one use a before_filter in application_controller.rb so current_user is always set?
The problem is that SOMETIMES you don't need the current_user, and therefore it's uncessary to do a query for every request.
This way it only do it when the ability gets called.
:-) But I am trying to figure out wether this is the right approach?
The text was updated successfully, but these errors were encountered:
Does the guest user serve any other purpose outside of the Ability class? If not you can just define an instance of User in memory instead of creating one to the database each time.
def initialize(user)
user ||= User.new(...)
end
However, if the guest user does need to be saved to the database and stored in the session then it should happen outside of the Ability class. This is because Ability knows nothing of the current request/session.
I recommend doing this in the current_user method. Something like.
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
@current_user ||= User.create!(....) # make guest user
@current_user
end
This way the guest user will only be created upon fetching the current user. Does this work for you?
I got
This is in the Ability.rb
Is that the correct way to go? Or should one use a before_filter in application_controller.rb so current_user is always set?
The problem is that SOMETIMES you don't need the current_user, and therefore it's uncessary to do a query for every request.
This way it only do it when the ability gets called.
:-) But I am trying to figure out wether this is the right approach?
The text was updated successfully, but these errors were encountered: