Created
August 2, 2014 17:36
-
-
Save anonymous/134584a9cc552922846a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
u = User.create | |
id = u.id | |
# The above should and does essentially do u.sessions.create() | |
# I expect after initializing this for it to test !session_valid? == false && !new_record? == false | |
u = User.find(id) | |
# I expect after initializing this for it to test !session_valid? == false && !new_record? == false | |
u.sessions.destroy_all | |
u = User.find(id) | |
# I expect after initializing this for it to test !session_valid? == true && !new_record? == true | |
# and then "recreate" the User and return the new user ...... | |
# It seems to create an infinite loop and I am having trouble following the logic of this .... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
after_create :create_valid_session | |
after_initialize :redeploy_if_session_invalid | |
has_many :sessions | |
def session_valid? | |
self.sessions.present? | |
end | |
private | |
def redeploy_if_session_invalid | |
if !session_valid? && !new_record? | |
puts "-" * 30 | |
puts "!session_valid?: #{!session_valid?}" | |
puts "!new_record?: #{!new_record?}" | |
puts "-" * 30 | |
my_attributes = self.attributes | |
my_attributes.delete 'id' | |
destroy | |
self.class.create my_attributes | |
end | |
end | |
def create_valid_session | |
self.sessions.create | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment