questions about cookies when bridging together two rails apps

Hey all,

I am looking through this example Rails app where a user session is stored in cookie so user signs up in one rails app and navigates to another while still being signed in as unique user. I come across this line of code where I don't understand where some of these methods are coming from:

    @session = Session.create!(:user => @user)     cookies[:session_token] = {:value => @session.token, :domain => Settings.session_token_domain}

1) I look in Session class and there is no create! class method. Yet it works and doesn't throw an exception.

I do understand when that create! method is called, the constructor is initialized first and generates a random number and assigns it to the token property (self.token) of the specific instance. And then stores the instance to the @session instance variable.

2) I did a global search of app and found no property or method called "session_token_domain", although there is a Session class but session_token_domain is not declared anywhere in it. The only other location is in settings.yml, which has something like this:

production:   <<: *default_settings   session_token_domain: xxxxx

So I'm not sure why this doesn't throw an exception either.

3) Finally I see that Rails supports a hash called cookies that can pass strings or symbols. Here a key is created and a value which contains subhashes is passed into it. So we store a unique session token for user. What value does this add rather than just assigning the user id in the sessions hash which is actually done in the same method:

session[:user_id] = @user.id

Why store them both?

Thanks for response

Hey all,

I am looking through this example Rails app where a user session is stored in cookie so user signs up in one rails app and navigates to another while still being signed in as unique user. I come across this line of code where I don't understand where some of these methods are coming from:

@session = Session\.create\!\(:user =&gt; @user\)
cookies\[:session\_token\] = \{:value =&gt; @session\.token, :domain =&gt;

Settings.session_token_domain}

1) I look in Session class and there is no create! class method. Yet it works and doesn't throw an exception.

With so little context it is hard to say.

I do understand when that create! method is called, the constructor is initialized first and generates a random number and assigns it to the token property (self.token) of the specific instance. And then stores the instance to the @session instance variable.

2) I did a global search of app and found no property or method called "session_token_domain", although there is a Session class but session_token_domain is not declared anywhere in it. The only other location is in settings.yml, which has something like this:

Again, we know nothing about your app. At a wild guess, maybe the settings class reads settings.yml and created methods for all the attributes defined in there.

Fred

Frederick Cheung wrote in post #995797:

Settings.session_token_domain}

1) I look in Session class and there is no create! class method. Yet it works and doesn't throw an exception.

With so little context it is hard to say.

It inherits from ActiveRecord and I know ActiveRecord doesn't contain a create! method. The only possibility could be then a gem that works as a singleton. But what I would love to do is something like: logger.info Session.create! and then get output that says that this method is defined in such and such class, kind of like a callstack. But of course logger.info or logger.error just tells you what the value holds, not where it was declared and initialized.

Frederick Cheung wrote in post #995797:

>> Settings.session_token_domain}

>> 1) I look in Session class and there is no create! class method. Yet it >> works and doesn't throw an exception.

> With so little context it is hard to say.

It inherits from ActiveRecord and I know ActiveRecord doesn't contain a create! method.

Wrongo. ActiveRecord::Validations::ClassMethods

The only possibility could be then a gem that works as a singleton. But what I would love to do is something like: logger.info Session.create! and then get output that says that this method is defined in such and such class, kind of like a callstack. But of course logger.info or logger.error just tells you what the value holds, not where it was declared and initialized.

You could step into it with the debugger.

Fred