Hey all I have a main_controller which has the basic scaffold methods. It operates on a single table, entries and on the entry.rb model. This is its migration file, def self.up create_table :entries do |t| t.column :title, :string t.column :created, :timestamp end end When I scaffolded it RoR automatically created a new.rhtml which inserted both the title and the timestamp via a form. Now I''ve removed the timestamp part from the form because I want each entry to be persisted with the current date and time. My main_controller which handles the persistance has the following new and create methods def new @entry = Entry.new end def create @entry = Entry.new(params[:entry]) if @entry.save flash[:notice] = ''Entry was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end Now what I don''t understand is where in the code the actual form data is saved, I''m from a Java background so this is all very new to me. First off, there is no initialize method for entry.rb, so how is the title field for the object set? Also, there are no fields declared in entry.rb, so how/where can I set the default timestamp for each entry.rb object to the current date/time? This is entry.rb class Entry < ActiveRecord::Base validates_presence_of :created 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 -~----------~----~----~----~------~----~------~--~---
hi, the initialize is when you do this @entry = Entry.new that new ''object'' @entry then gets passed to your view called ''new''.rhtml. ''if @entry.save'' is short for saying ''save @entry if @entry can be saved''..else redisplay the new.rhtml. entry model is the singular term for the entries table. takes awhile to get used to but starts to make sense eventually. i came from a c++ and java background so getting used to things that magically happens to awhile for me to get used to. Nima wrote:> Hey all > > I have a main_controller which has the basic scaffold methods. It > operates on a single table, entries and on the entry.rb model. This is > its migration file, > > def self.up > create_table :entries do |t| > t.column :title, :string > t.column :created, :timestamp > end > end > > When I scaffolded it RoR automatically created a new.rhtml which > inserted both the title and the timestamp via a form. Now I''ve removed > the timestamp part from the form because I want each entry to be > persisted with the current date and time. > > My main_controller which handles the persistance has the following new > and create methods > > def new > @entry = Entry.new > end > > def create > @entry = Entry.new(params[:entry]) > if @entry.save > flash[:notice] = ''Entry was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > Now what I don''t understand is where in the code the actual form data is > saved, I''m from a Java background so this is all very new to me. First > off, there is no initialize method for entry.rb, so how is the title > field for the object set? Also, there are no fields declared in > entry.rb, so how/where can I set the default timestamp for each entry.rb > object to the current date/time? > > This is entry.rb > > class Entry < ActiveRecord::Base > > validates_presence_of :created > > 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 -~----------~----~----~----~------~----~------~--~---
Nima wrote:> Now what I don''t understand is where in the code the actual form data is > saved, I''m from a Java background so this is all very new to me. First > off, there is no initialize method for entry.rb, so how is the title > field for the object set?There actually is an initialize method for your Entry class; it''s inherited from ActiveRecord::Base rather than being declared in entry.rb. The first time your application accesses your Entry model, ActiveRecord issues a database call to read the database structure of your "entries" table. From that, it determines what columns the table has, and therefore, what attributes (fields) the object will have. (These attributes are actually stored in a hash instance variable called @attributes.)> Also, there are no fields declared in > entry.rb, so how/where can I set the default timestamp for each entry.rb > object to the current date/time?In general, you can declare a "before_save" callback method in your model to modify or set some data before a record is saved. In this particular case, though, the simplest way is to simply rename your column to "created_at". ActiveRecord will automatically recognize this as a reserved or "magic" column name, and will set it to the current timestamp whenever a record is created, without any other programming on your part. -- 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 -~----------~----~----~----~------~----~------~--~---