John Merlino
2011-Apr-29 02:04 UTC
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-29 15:36 UTC
Re: questions about cookies when bridging together two rails apps
On Apr 29, 3:04 am, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. >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> 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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Apr-29 21:17 UTC
Re: questions about cookies when bridging together two rails apps
Frederick Cheung wrote in post #995797:> On Apr 29, 3:04am, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-29 22:30 UTC
Re: questions about cookies when bridging together two rails apps
On Apr 29, 10:17 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #995797: > > > On Apr 29, 3:04am, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> 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. http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-create-21> 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> > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.