On Dec 7, 7:51 pm, shreko <[email protected]> wrote:
> I'm fairly new to all things ruby. Working through padrino blog
> tutorial using sequel (3.30), mysql2 and padrino 0.10.5
> In a migration to add account_id to Post I have the following
>
> class AddAccountToPost < Sequel::Migration
> def up
> alter_table :posts do
> add_column :account_id, Integer
> end
>
> #and assigns a user to all existing posts
> first_account = Account.first
> Post.all.each { |p| p.update(:account_id => first_account.id)}
>
> end
>
> def down
> alter_table :posts do
> drop_column :account_id
> end
> end
> end
>
> The first part of the up migration add_column succeeds, but the second
> part of assigning an user account to all existing posts fails with
> rake message: "method account_id= doesn't exist"
>
> If I log on the padrino console the code
>
> first_account = Account.first
> Post.all.each { |p| p.update(:account_id => first_account.id)}
>
> executes without errors, it works.
>
> What is wrong here, please?
It's generally a bad idea to use models in migrations. Do this
instead:
from(:posts).update(:account_id => from(:accounts).get(:id))
In addition to being the correct way to do this, it's also going to be
much faster.
You should also switch to using using the migration DSL:
Sequel.migration do
up do
...
end
down do
...
end
end
In case you are wondering why you got the error message originally,
it's because when the Post class was loaded, the account_id column did
not yet exist in it. You can keep your existing code if you do the
following after the alter_table:
Post.dataset = Post.dataset
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.