attr_accessor :password dis is small bit of code i have seen in a tutorial . in that example there is no field called password in the database, so how can rails understand its the password field of my database. in my database i have given password field as hashed_password i got an explanation form the tutorial as given below " The attr_accessor statement specifies the attribute password. You may have noticed that this doesn’t actually exist as a field in the database migration. This declares that password is an attribute of the model even though it doesn’t exist as a database field. The model will be able to use the data from this field to create the hashed_password field. You will notice that this is done by the before_save filter. This will simply set the hashed_password field to be a hash of the clear password which, as stated in our specification, we can compare to the stored hashed_password value to authenticate a user’s login credentials. " but i ddn understand clearly... can any explain in better way! -- Posted via http://www.ruby-forum.com/.
On Wed, Oct 14, 2009 at 2:34 AM, Ralu rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > attr_accessor :password > dis is small bit of code i have seen in a tutorial . > > in that example there is no field called password in the database, so > how can rails understand its the password field of my database. in my > database i have given password field as hashed_password > > i got an explanation form the tutorial as given below > " The attr_accessor statement specifies the attribute password. You > may have noticed that > this doesn’t actually exist as a field in the database migration. This > declares that password is an > attribute of the model even though it doesn’t exist as a database field. > The model will be able to > use the data from this field to create the hashed_password field. You > will notice that this is done > by the before_save filter. This will simply set the hashed_password > field to be a hash of the clear > password which, as stated in our specification, we can compare to the > stored hashed_password > value to authenticate a user’s login credentials. > " > > but i ddn understand clearly... can any explain in better way! >Where can one find this example? -Conrad> -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Wed, Oct 14, 2009 at 2:34 AM, Ralu rm > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > >> may have noticed that >> value to authenticate a user’s login credentials. >> " >> >> but i ddn understand clearly... can any explain in better way! >> > > Where can one find this example? > > -Conradi got dat from tutorial . for me the expalnation was not dat gud so can any one help me with dat -- Posted via http://www.ruby-forum.com/.
On Wed, Oct 14, 2009 at 3:21 AM, Ralu rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Conrad Taylor wrote: > > On Wed, Oct 14, 2009 at 2:34 AM, Ralu rm > > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > > > >> may have noticed that > >> value to authenticate a user’s login credentials. > >> " > >> > >> but i ddn understand clearly... can any explain in better way! > >> > > > > Where can one find this example? > > > > -Conrad > > i got dat from tutorial . for me the expalnation was not dat gud > so can any one help me with dat >Where''s the complete class?> -- > 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Wed, Oct 14, 2009 at 3:21 AM, Ralu rm > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > >> >> >> > >> > Where can one find this example? >> > >> > -Conrad >> >> i got dat from tutorial . for me the expalnation was not dat gud >> so can any one help me with dat >> > > Where''s the complete class?require ''digest/sha2'' class User < ActiveRecord::Base attr_protected :hashed_password, :enabled attr_accessor :password validates_presence_of :username validates_presence_of :email validates_presence_of :password, :if => :password_required? validates_presence_of :password_confirmation, :if => :password_required? validates_confirmation_of :password, :if => :password_required? validates_uniqueness_of :username, :case_sensitive => false validates_uniqueness_of :email, :case_sensitive => false validates_length_of :username, :within => 3..64 validates_length_of :email, :within => 5..128 validates_length_of :password, :within => 4..20, :if => :password_required? validates_length_of :profile, :maximum => 1000 def before_save self.hashed_password = User.encrypt(password) if !self.password.blank? end def password_required? self.hashed_password.blank? || !self.password.blank? end def self.encrypt(string) return Digest::SHA256.hexdigest(string) end end -- Posted via http://www.ruby-forum.com/.
On Wed, Oct 14, 2009 at 4:19 AM, Ralu rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Conrad Taylor wrote: > > On Wed, Oct 14, 2009 at 3:21 AM, Ralu rm > > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > > > >> >> > >> > > >> > Where can one find this example? > >> > > >> > -Conrad > >> > >> i got dat from tutorial . for me the expalnation was not dat gud > >> so can any one help me with dat > >> > > > > Where''s the complete class? > > require ''digest/sha2'' > class User < ActiveRecord::Base > attr_protected :hashed_password, :enabled > attr_accessor :password > validates_presence_of :username > validates_presence_of :email > validates_presence_of :password, :if => :password_required? > validates_presence_of :password_confirmation, :if => > :password_required? > validates_confirmation_of :password, :if => :password_required? > validates_uniqueness_of :username, :case_sensitive => false > validates_uniqueness_of :email, :case_sensitive => false > validates_length_of :username, :within => 3..64 > validates_length_of :email, :within => 5..128 > validates_length_of :password, :within => 4..20, :if => > :password_required? > validates_length_of :profile, :maximum => 1000 > def before_save > self.hashed_password = User.encrypt(password) if > !self.password.blank? > end > def password_required? > self.hashed_password.blank? || !self.password.blank? > end > def self.encrypt(string) > return Digest::SHA256.hexdigest(string) > end > end > >''attr_accessor :password'' will generate two instance methods for class User similar to the following: def password=(val) @password = val end def password @password end From the above, the instance variable, password, is brought into scope within the User class when ''@password = val'' is executed. In Ruby, you can add an instance variable to a class by simply assigning a value to instance variable within an instance method. Then executing it. Finally, I would recommend learning more about Ruby by reading "Programming Ruby" or "Programming Ruby 1.9". Good luck, -Conrad> -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Wed, Oct 14, 2009 at 4:19 AM, Ralu rm > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > >> >> >> validates_presence_of :username >> :password_required? >> end >> end >> >> > ''attr_accessor :password'' will generate two instance methods for class > User > similar > to the following: > > def password=(val)thanks .....ill go for that -- Posted via http://www.ruby-forum.com/.