Hi, i am trying to overwrite a value in a model so that the new value is beeing saved in the database instead of the one the user typed into the form. this is my controller class Movie < ActiveRecord::Base def self.creation_date=(a) self.creation_date = "2000-01-01" end end (it''s only some testing for getting a feeling for the basic way ror works...) the create form is generated by the standard scaffold :movie in my admin controller. the new movie is beeing created but it uses the value from the form, not my new "overwritten" one. it''s obviously a simple question but iam stucked now for about 2.5 houres :( shame on me... any ideas? Greetings, Pascal --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> any ideas?Try writing your overwrite code in the after_update or after_save callback method. In this method, write straight to the database via something like: def after_update model.connection.execute( "UPDATE model set creation_date=now() WHERE id=" << model.id) end -- 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Apr-18 21:27 UTC
Re: Beginner question - overwrite a value in a model
> Hi, > > i am trying to overwrite a value in a model so that > the new value is beeing saved in the database instead of the one the > user > typed into the form. > > this is my controllerI think you meant model... anyway... class Movie < ActiveRecord::Base before_save :fudge_creation_date ... def fudge_creation_date self.creation_date = Date.parse(''2001-01-1'') end end Then every time you save the record it will do what you want.> > class Movie < ActiveRecord::Base > > def self.creation_date=(a) > self.creation_date = "2000-01-01" > end > > end > > (it''s only some testing for getting a feeling for the basic way > ror works...) > > the create form is generated by the standard scaffold :movie > in my admin controller. > > the new movie is beeing created but it uses the value > from the form, not my new "overwritten" one. > > it''s obviously a simple question but iam stucked now for > about 2.5 houres :( shame on me... > > any ideas? > > Greetings, > > Pascal > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pascal Friederich
2007-Apr-18 21:30 UTC
Re: Beginner question - overwrite a value in a model
thanks (yes, model, not controller :) ), but why does it work in the example at "agile webdevelopement with ror" at the model http://media.pragprog.com/titles/rails2/code/depot_p/app/models/user.rb they are setting the hashed_password value in the password=(pwd) method. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Apr 18, 2007 at 13:55 -0700, Pascal Friederich wrote:> class Movie < ActiveRecord::Base > > def self.creation_date=(a) > self.creation_date = "2000-01-01" > end > > end# skipped> the new movie is beeing created but it uses the value > from the form, not my new "overwritten" one.maybe # instance method should be without `self.` prefix! def creation_date=(a) self[:creation_date] = "2000-01-01" end ? -- wbr, sh0ckfile. FileForum moders team http://www.fforum.ru --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > def self.creation_date=(a) > > self.creation_date = "2000-01-01" > > endNotice that when you add self.some_method you are creating a "Class" method rather than an "Instance" method. def password=(pwd) @password = pwd return if pwd.blank? create_new_salt self.hashed_password = User.encrypted_password(self.password, self.salt) end private def self.encrypted_password(password, salt) string_to_hash = password + "wibble" + salt # ''wibble'' makes it harder to guess Digest::SHA1.hexdigest(string_to_hash) end See how the AWDWR book is defining the "password=(pwd)" method to be an instance method (no self.) User.encrypted_password(..., ...) is defined as a Class method. Notice how they call the class method on the class not a reference to an instance. On Apr 18, 5:31 pm, Sh0Ck_filE <sh0ckf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Apr 18, 2007 at 13:55 -0700, Pascal Friederich wrote: > > class Movie < ActiveRecord::Base > > > def self.creation_date=(a) > > self.creation_date = "2000-01-01" > > end > > > end > # skipped > > the new movie is beeing created but it uses the value > > from the form, not my new "overwritten" one. > > maybe > # instance method should be without `self.` prefix! > def creation_date=(a) > self[:creation_date] = "2000-01-01" > end > > ? > > -- > wbr, sh0ckfile. > FileForum moders teamhttp://www.fforum.ru--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''m also curious why you want all your creation dates to be "2000-01-01," without any way to change it? What are you really wanting to do here? Are you looking for behavior that time stamps record creation? If so then Rails will happily do that automatically for you. All you need to do is name your column created_on and it will automatically be set by the framework to the current date. You can also use created_at to get a full date and time, update_on/updated_at to have a time stamp update every time the record changes. On Apr 18, 6:22 pm, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > def self.creation_date=(a) > > > self.creation_date = "2000-01-01" > > > end > > Notice that when you add self.some_method you are creating a "Class" > method rather than an "Instance" method. > > def password=(pwd) > @password = pwd > return if pwd.blank? > create_new_salt > self.hashed_password = User.encrypted_password(self.password, > self.salt) > end > > private > > def self.encrypted_password(password, salt) > string_to_hash = password + "wibble" + salt # ''wibble'' makes it > harder to guess > Digest::SHA1.hexdigest(string_to_hash) > end > > See how the AWDWR book is defining the "password=(pwd)" method to be > an instance method (no self.) > > User.encrypted_password(..., ...) is defined as a Class method. > Notice how they call the class method on the class not a reference to > an instance. > > On Apr 18, 5:31 pm, Sh0Ck_filE <sh0ckf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Wed, Apr 18, 2007 at 13:55 -0700, Pascal Friederich wrote: > > > class Movie < ActiveRecord::Base > > > > def self.creation_date=(a) > > > self.creation_date = "2000-01-01" > > > end > > > > end > > # skipped > > > the new movie is beeing created but it uses the value > > > from the form, not my new "overwritten" one. > > > maybe > > # instance method should be without `self.` prefix! > > def creation_date=(a) > > self[:creation_date] = "2000-01-01" > > end > > > ? > > > -- > > wbr, sh0ckfile. > > FileForum moders teamhttp://www.fforum.ru--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pascal Friederich
2007-Apr-18 22:31 UTC
Re: Beginner question - overwrite a value in a model
thanks for the, i think i understood this now :) but the thing that makes me pain in my brain is that self.hashed_password = User.encrypted...xxx asignment is to self.hashed_password and it works, the value that is written to the table at the "hashed_password" column is the one returned from user.encrypted...xxx. it is functional equal to my new method which doesn''t work (the solution with bevore_save does work but that doesn''t solve the riddle the AWDWROR from book example in comparison to my little method) def creation_date=(cd) #same as self.hashed_password = ... // like AWDWROR team does self.creation_date = Date.parse(''2000-01-01'') end (sorry for my bad / hard to read english sentences.. :( ) On 19 Apr., 00:22, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > def self.creation_date=(a) > > > self.creation_date = "2000-01-01" > > > end > > Notice that when you add self.some_method you are creating a "Class" > method rather than an "Instance" method. > > def password=(pwd) > @password = pwd > return if pwd.blank? > create_new_salt > self.hashed_password = User.encrypted_password(self.password, > self.salt) > end > > private > > def self.encrypted_password(password, salt) > string_to_hash = password + "wibble" + salt # ''wibble'' makes it > harder to guess > Digest::SHA1.hexdigest(string_to_hash) > end > > See how the AWDWR book is defining the "password=(pwd)" method to be > an instance method (no self.) > > User.encrypted_password(..., ...) is defined as a Class method. > Notice how they call the class method on the class not a reference to > an instance.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pascal Friederich
2007-Apr-18 22:47 UTC
Re: Beginner question - overwrite a value in a model
this is realy nice. and in fact, this was one of the things i tried to figure out. awsome! the other thing is that i just wanted to now -if- (ok i was really shure it -is-) i can and -how- i can set values to a dynamic / static default value inside a model. as you see, iam really new to ruby (and the mvc idear in general..) where do -you- find such things like the "created_on and it will automatically" trick? Ok, api will provide these information but a) it find it difficult to read and b) how to find s.th. you dont even know it exists? :) On 19 Apr., 00:30, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m also curious why you want all your creation dates to be > "2000-01-01," without any way to change it? > > What are you really wanting to do here? Are you looking for behavior > that time stamps record creation? If so then Rails will happily do > that automatically for you. All you need to do is name your column > created_on and it will automatically be set by the framework to the > current date. You can also use created_at to get a full date and > time, update_on/updated_at to have a time stamp update every time the > record changes. > > On Apr 18, 6:22 pm, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > def self.creation_date=(a) > > > > self.creation_date = "2000-01-01" > > > > end > > > Notice that when you add self.some_method you are creating a "Class" > > method rather than an "Instance" method. > > > def password=(pwd) > > @password = pwd > > return if pwd.blank? > > create_new_salt > > self.hashed_password = User.encrypted_password(self.password, > > self.salt) > > end > > > private > > > def self.encrypted_password(password, salt) > > string_to_hash = password + "wibble" + salt # ''wibble'' makes it > > harder to guess > > Digest::SHA1.hexdigest(string_to_hash) > > end > > > See how the AWDWR book is defining the "password=(pwd)" method to be > > an instance method (no self.) > > > User.encrypted_password(..., ...) is defined as a Class method. > > Notice how they call the class method on the class not a reference to > > an instance. > > > On Apr 18, 5:31 pm, Sh0Ck_filE <sh0ckf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Wed, Apr 18, 2007 at 13:55 -0700, Pascal Friederich wrote: > > > > class Movie < ActiveRecord::Base > > > > > def self.creation_date=(a) > > > > self.creation_date = "2000-01-01" > > > > end > > > > > end > > > # skipped > > > > the new movie is beeing created but it uses the value > > > > from the form, not my new "overwritten" one. > > > > maybe > > > # instance method should be without `self.` prefix! > > > def creation_date=(a) > > > self[:creation_date] = "2000-01-01" > > > end > > > > ? > > > > -- > > > wbr, sh0ckfile. > > > FileForum moders teamhttp://www.fforum.ru--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pascal, 1) self.hashed_password = User.encrypted...xxx 2) asignment is to self.hashed_password and it works, the value Here''s the bit that you are missing. In line 1 above self.hashed_password is NOT the definition of the hashed_password method. You do not see something like: def self.hashed_password ... ... end the hashed_password is an instance method (not a class method) that is added to the User model by Rails. def self.authenticate(name, password) user = self.find_by_name(name) # <=== geting a reference to an instance of User if user expected_password = encrypted_password(password, user.salt) if user.hashed_password != expected_password # <=== referencing hashed_password on an instance of User user = nil end end user end On Apr 18, 6:31 pm, Pascal Friederich <pau...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> thanks for the, i think i understood this now :) > > but the thing that makes me pain in my brain is that > self.hashed_password = User.encrypted...xxx > asignment is to self.hashed_password and it works, the value > that is written to the table at the "hashed_password" column is the > one returned from user.encrypted...xxx. > > it is functional equal to my new method which doesn''t work > (the solution with bevore_save does work but that doesn''t solve the > riddle the AWDWROR from book example in comparison to my > little method) > > def creation_date=(cd) > #same as self.hashed_password = ... // like AWDWROR team does > self.creation_date = Date.parse(''2000-01-01'') > end > > (sorry for my bad / hard to read english sentences.. :( ) > > On 19 Apr., 00:22, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > def self.creation_date=(a) > > > > self.creation_date = "2000-01-01" > > > > end > > > Notice that when you add self.some_method you are creating a "Class" > > method rather than an "Instance" method. > > > def password=(pwd) > > @password = pwd > > return if pwd.blank? > > create_new_salt > > self.hashed_password = User.encrypted_password(self.password, > > self.salt) > > end > > > private > > > def self.encrypted_password(password, salt) > > string_to_hash = password + "wibble" + salt # ''wibble'' makes it > > harder to guess > > Digest::SHA1.hexdigest(string_to_hash) > > end > > > See how the AWDWR book is defining the "password=(pwd)" method to be > > an instance method (no self.) > > > User.encrypted_password(..., ...) is defined as a Class method. > > Notice how they call the class method on the class not a reference to > > an instance.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---