Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method 'enum' when migrating #49

Open
unikitty37 opened this issue Jun 5, 2012 · 8 comments
Open

undefined method 'enum' when migrating #49

unikitty37 opened this issue Jun 5, 2012 · 8 comments

Comments

@unikitty37
Copy link

I've just installed enumerated_attribute (using rails 3.2.1) by the standard method of adding it to the Gemfile and running bundle. I have the following migration

class AddReadableByToArticle < ActiveRecord::Migration
  def change
    change_table :articles do |t|
      t.enum :readable_by
    end
  end
end

and the following as models/article.rb:

class Article < ActiveRecord::Base
  enum_attr :readable_by, %w(^draft branch members public)

According to the README, this should work, no?

But I get undefined methodenum' for #ActiveRecord::ConnectionAdapters::Table:0x007fbc533e87f0` when I try to migrate.

Are the docs out of date? I notice that the README still refers to using rake gems:install to install the gem, so I'm wondering if the docs no longer reflect reality.

@AlbertGrimley
Copy link

Try adding a require in your environment.rb

require "enumerated_attribute"

@unikitty37
Copy link
Author

Thanks — it hasn't had any effect, though, even when I've put the require directly into the migration in question. I've remembered to restart the rails server as well :)

I can create the column as a t.string, but then it's actually created as VARCHAR(255) rather than ENUM, which removes the point of using the gem in the first place — I need to have the field values restricted at the database level, rather than just managed by rails.

@AlbertGrimley
Copy link

I'm now noticing the "change_table" call you're making. Mine are all create_table calls, within the change method for the migration. Change your migration to be
def change
create_table :articles do |t|
...

Also, the t.enum creates a column of type varchar(255) and there is no value checking in the database. The value checking is performed in the application later in the gem within rails.
After running the migration successfully, you'll see that schema.rb will show t.string for the column that was originally t.enum in the migration.

@AlbertGrimley
Copy link

And if the column already exists and you're simply trying to change the type, I don't think you have to do this. As I mentioned above, the type is still varchar(255) (string) so I think the migration is unnecessary.

@unikitty37
Copy link
Author

The reason it's not create_table is because this migration is adding a column to an existing table :)

The comment about creating as a string was just from looking at an earlier project where I'd successfully used enumerated_attribute, but I was creating the columns as strings in that — though if enumerated_attribute only adds VARCHAR(255) columns anyway, I guess replacing t.enum with t.string will get things working…

Thanks for your help!

@AlbertGrimley
Copy link

Interesting. I've only added columns using this construct:

def change
add_column :table_foo, :column_bar, :string
end

Try :enum instead of :string with this construct.

@unikitty37
Copy link
Author

Afraid not — it gets as far as mysql this time, though:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1: ALTER TABLE `articles` ADD `readable_by` enum

Looking at the log, that's the entire statement. Obviously, it needs to know the values of the enum, but it's not very DRY to repeat them in the migration and the model.

I think I'll stick to a string.

@AlbertGrimley
Copy link

The gem should have translated that for you. Something is not right with the inclusion of the gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants