Skip to content

Instantly share code, notes, and snippets.

@advorak
Forked from anonymous/sample_script.rb
Created August 2, 2014 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advorak/f6449e6bc6f1cd703a36 to your computer and use it in GitHub Desktop.
Save advorak/f6449e6bc6f1cd703a36 to your computer and use it in GitHub Desktop.
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 ....
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