rubyonrailsin

A Ruby and Rails talk

Saturday, March 13, 2010


Re: [Rails] Coding a migration to change a column's attribute

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On 13 March 2010 19:35, RichardOnRails
<RichardDummyMailbox58407@uscomputergurus.com> wrote:
> Hi All,
>
> I want a migration to change the Amount column of the Expense table to
> the precision/scale to 10/2.
>
> It's easy to say in one sentence.  It's a little harder to do.
>
> 1 Ran: ruby script/generate migration ChangeExpenseTbl_AmountCol
> 2.Got db/migrate/20100313180401_change_expense_tbl_amount_col.rb
> 3.Modified the migration as follows:
>
> class ChangeExpenseTblAmountCol < ActiveRecord::Migration
>  def self.up
>    class Expense
>      change_column :amount, decimal :precision=>10, scale=>2
>    end
>  end
>
>  def self.down
>  end
> end
>
> This looks like it could be DRYed to this:
>
> class Expense < ActiveRecord::Migration
>  def self.up
>      change_column :amount, decimal :precision=>10, scale=>2
>  end
>
>  def self.down
>  end
> end
>
> Do either of these look correct?

Why not try it and see? Provided you have a dump of the database (and
you do have your code under source control in git (or svn) don't you)
then if it messes something up no harm is done.

Did you check the params required for change_column from the docs (
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)? It
looks to me like you have a missing parameter.

Colin

--
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@googlegroups.com.
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.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive