electronick / enum_column Goto Github PK
View Code? Open in Web Editor NEWEnum column support for Rails 3
License: MIT License
Enum column support for Rails 3
License: MIT License
I banged my head against the screen while trying to get validates :gender, :inclusion => { :in => %w{male female} }
to work, while :gender was an :enum field. But validations just don't pass. When doing the same with a normal :string field, everything works.
Migration:
class CreatePlayers < ActiveRecord::Migration
def change
create_table :players do |t|
t.string :name
t.enum :gender, :limit => [:male, :female], :default => :male
t.string :gender2
t.timestamps
end
end
end
Model:
class Player < ActiveRecord::Base
validates :gender, :inclusion => { :in => %w{male female} }
validates :gender2, :inclusion => { :in => %w{male female} }
end
Here some code which demonstrates the problem:
1.9.3p0 :001 > p = Player.new
=> #<Player id: nil, name: nil, gender: :male, gender2: nil, created_at: nil, updated_at: nil>
1.9.3p0 :002 > p.valid?
=> false
1.9.3p0 :003 > p.errors
=> #<ActiveModel::Errors:0x007ff55601e1d8 @base=#<Player id: nil, name: nil, gender: :male, gender2: nil, created_at: nil, updated_at: nil>, @messages={:gender=>["ist nicht in der Liste vorhanden!"], :gender2=>["ist nicht in der Liste vorhanden!"]}>
1.9.3p0 :004 > p.gender = 'male'
=> "male"
1.9.3p0 :005 > p.gender2 = 'male'
=> "male"
1.9.3p0 :006 > p.valid?
=> false
1.9.3p0 :007 > p.errors
=> #<ActiveModel::Errors:0x007ff55601e1d8 @base=#<Player id: nil, name: nil, gender: :male, gender2: "male", created_at: nil, updated_at: nil>, @messages={:gender=>["ist nicht in der Liste vorhanden!"]}>
I'm not certain, but I suspect there may be some incompatibilities between enum_column and the updated mysql2 gems that manifest in Rails 3.1.0:
In a Rails 3.0.9 project, I can "rake db:schema:dump" and get nice enum columns from the database. In my Rails 3.1.0 project, "rake db:schema:dump" generates the same broken schema.rb files as you'd get without using the enum_column library.
I suspect that, since Rails 3.1.0 apparently includes the MySQL adapter by default, and the mysql2 gem no longer includes it (as evidenced by the fact that Rails 3.0.9 doesn't work if you've got a version of mysql2 >= 0.3) this has changed things just enough that the dumper isn't taking advantage of enum_column anymore...
I'll keep poking, but if anyone can repro this, or can figure out what I've done wrong, I'll be very happy. :)
Even though I would like to use the enum_select option I end up having to do.
f.select(:color, Enumeration.columns_hash['color'].limit, :prompt => "Choose Color")
instead of
f.enum_select(:color, :prompt => "Choose Color")
because the latter creates Choose Color and .
c:/idjet/Ruby192/lib/ruby/gems/1.9.1/gems/enum_column3-0.1.3/lib/enum/enum_adapter.rb:8:in <top (required)>': uninitialized constant ActiveRecord::ConnectionAdapters::Mysq lColumn (NameError) from c:/idjet/Ruby192/lib/ruby/gems/1.9.1/gems/enum_column3-0.1.3/lib/enum_column.rb:6:in
block (2 levels) in class:EnumColumnRailtie'
from c:/idjet/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/lazy_load_hooks.rb:36:in instance_eval' from c:/idjet/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/lazy_load_hooks.rb:36:in
execute_hook'
from c:/idjet/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
I added enum_column3 to my gemfile for a rails 3.01 project, installed it with bundler, and created and ran a migration. I confirmed in the rails console that the column was created as expected. Here is the error output:
undefined local variable or method `enum' for #ActionView::Helpers::InstanceTag:0x29e7f94
Extracted source (around line #24):
21:
22:
I have traced it to line 34 in active_record_helper.rb:
31: if (value = self.object.__send__(method_name.to_sym))
32: options[:selected] ||= value.to_s
33: else
34: options[:include_blank] = enums.allows_nil? if options[:include_blank].nil?
35: end
enums is not defined or referenced anywhere outside line 34.
I'm attempting to use this gem in a project which uses ActiveRecord in isolation, but it doesn't seem possible due to the if ::Rails::Railtie
condition wrapping the code in enum_column.rb
. Without this, my schema dumps don't pick up the enum.
Hey! I'm trying to install from git source and things worked fine until recently...I now get:
Could not find gem 'enum_column (>= 0)' in git://github.com/electronick/enum_column.git (at master).
I think in messing around with the Gemspec recently it might have broken. In addition, I looked at your commit history and it seems to go from 0.7.x to 0.1.x, I don't think that's good :-P Hopefully that's enough for you to track it down. Thanks again for the awesome gem!
Is there a way to add enum's to existing tables?
Also If I need to add a new value to the enum what is the syntax.
I want to change an enum column from
From
status ('pending', 'active')
To
status ('pending', 'active', 'canceled')
and can I do something like this?
add_column :status, :enum, :limit => [:casual, :formal, :wild, :sports, :neutral, :colorful]
enum_column3-0.1.4/lib/enum/enum_adapter.rb:30:in block in <top (required)>': undefined method
type_cast' for class `ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::Column' (NameError)
Hi
I have an issue here. I want to use AR in Sinatra (or Merb, for that matter), but the initialization of the gem is done in a Railstie block. Is it for some special reason like that? I would say that the only dependency of this gem is ActiveRecord (or mysql adapter, since enum is a mysql-only property), and therefore, by unrails it, you'd be having the gem used in more application (non-Rails as well).
By the way, my short-term solution was to put whatever was on the Railstie block in an initializer. A bit overkill, for now.
I get the following:
~/workspace/pro (master) $ rake db:init_partial --trace
rake aborted!
undefined method to_tag' for class
ActionView::Helpers::InstanceTag'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/enum_column-0.1.6/lib/enum_column/active_record_helper.rb:38:in <class:InstanceTag>' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/enum_column-0.1.6/lib/enum_column/active_record_helper.rb:37:in
module:Helpers'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/enum_column-0.1.6/lib/enum_column/active_record_helper.rb:3:in <module:ActionView>' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/enum_column-0.1.6/lib/enum_column/active_record_helper.rb:2:in
<top (required)>'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in require' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in
block in require'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in load_dependency' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in
require'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/enum_column-0.1.6/lib/enum_column.rb:10:in <top (required)>' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:68:in
require'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:68:in block (2 levels) in require' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:66:in
each'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:66:in block in require' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:55:in
each'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler/runtime.rb:55:in require' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@pro/gems/bundler-1.1.4/lib/bundler.rb:119:in
require'
/Users/aaron/workspace/pro/config/application.rb:63:in <top (required)>' /Users/aaron/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
require'
/Users/aaron/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in require' /Users/aaron/workspace/pro/Rakefile:5:in
<top (required)>'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in load' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in
load_rakefile'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in raw_load_rakefile' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:82:in
block in load_rakefile'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:81:in
load_rakefile'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in block in run' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in
standard_exception_handling'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in run' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/bin/rake:33:in
<top (required)>'
/Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in load' /Users/aaron/.rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in
Using the gem, all migrations work and the columns are being updated to Enum columns. (using mysql)
When I migrate the DB, the schema does not get dumped correctly and I get a warning that the table can't be dumped.
Once I change the field back to string, everything goes back to normal.
Using Ruby 1.9.2, Rails 3.0.9
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.