Hi, there. I got an error when I ran the migration to add a filed to database. In the migration file, def self.up change_table :contents do |t| t.add_string :new_names end end def self.down change_table :contents do |t| t.remove_column :new_names end end The error is: == AddMoreFieldsToContents: migrating =======================================-- change_table(:contents) rake aborted! An error has occurred, this and all later migrations canceled: undefined method `add_string'' for #<ActiveRecord::ConnectionAdapters::Table:0x2b b9b74> If I used add_column method, everything ran fine. I am running Rails 2.3.4. Any thoughts on the undefined method error? Thanks in advance. -- 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.
isn''t it t.add_column :new_names, :stirng http://api.rubyonrails.org/classes/ActiveRecord/Migration.html On 9 April 2010 22:44, Ichiro Saga <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, there. I got an error when I ran the migration to add a filed to > database. In the migration file, > > def self.up > change_table :contents do |t| > t.add_string :new_names > end > end > > def self.down > change_table :contents do |t| > t.remove_column :new_names > end > endisn''t it a > > The error is: > > == AddMoreFieldsToContents: migrating > =======================================> -- change_table(:contents) > rake aborted! > An error has occurred, this and all later migrations canceled: > > undefined method `add_string'' for > #<ActiveRecord::ConnectionAdapters::Table:0x2b > b9b74> > > > If I used add_column method, everything ran fine. I am running Rails > 2.3.4. Any thoughts on the undefined method error? > Thanks in advance. > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.
Ants Pants wrote:> isn''t it t.add_column :new_names, :stirng > > http://api.rubyonrails.org/classes/ActiveRecord/Migration.htmlThanks for reply, Ants Pants. It is from a book, Foundation Rails 2 by Eldon Alameda. Here is the quote from the book: The most recent version of Rails has added yet another shortcut for us in this process as well with the addition of a change_table method. We can use this method in a block in the same way that we did with the create_table method. It supports a number of new convenience methods within the block as well, such as add_XXX (which allows you to easily add a new column, for example, calling add_string to add a new string field), add_timestamps (which adds the magic created_at and updated_at datetime fields), remove_column (which allows you to remove a column and can accept multiple fields), and rename (which allows you to rename the table). So we could have rewritten our last migration like this: def self.up change_table :comments do |t| t.add_string :name, :email, :website end end def self.down change_table :comments do |t| t.remove_column :name, :email, :website end end Very nice and very DRY. I need to add 10+ columns to database, and I remember the cleaner way from the book. So I gave it a try. But somehow an error occured. -- 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.
On Apr 9, 10:19 pm, Ichiro Saga <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> So we could have rewritten our last migration like this: > def self.up > change_table :comments do |t| > t.add_string :name, :email, :website > endYou can do t.string :foo, :bar, :baz But I don''t think add_string has ever existed, similarly t.timestamps exists, but not t.add_timestamps. Looks like your book is just wrong Fred -- 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.