LoginSignup
1
0

More than 5 years have passed since last update.

Rails非標準の型を使うとFactoryGirlでエラーが出る

Last updated at Posted at 2012-04-05

migrationファイルで以下のようにした

class CreateHosts < ActiveRecord::Migration
  def change
    create_table :hosts do |t|
      t.string :name, :null => false, :limit => 255
      t.column :status, "enum('standby','working','destroyed','poweroff','maintenance')", :null => false, :default => 'maintenance'
      # ...
    end
  end
end

hostsテーブルにはstatusカラムがあり、statusが取りうる値の種類が初めから分かっているのでenumにした。

そしてHostモデルを以下のようにした

class Host < ActiveRecord::Base
  validates :status,
    :presence => true,
    :inclusion => {
      :in => %w[standby working destroyed poweroff maintenance]
    }
end

これ自体は問題なく動作どうさする。
しかしながら、FactoryGirlを使ってHostインスタンスを作ろうとすると

Can't convert Symbol to string

とRailsの奥深くで怒られる。
上記のstatusのvalidationが無いと問題ないしnameなどの標準の型に対するvalidationは問題ない。
結局諦めて組み込みの型を使うのが無難そうだと、判断した。

レールから外れる時は自己責任で、、、、ということですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0