1

我刚刚升级到 Mongoid3,我得到了很多这些错误:

TypeError:
   can't convert String into Integer

问题总是当我尝试构建一个属于另一个模型的模型并分配引用模型时:

@entry = FactoryGirl.create(:entry_image, user_id: @user.id, day: 18, month: 6)

它似乎是“user_id:@user.id”部分。我试过了:

@entry = FactoryGirl.build(:entry_image, day: 18, month: 6)
@entry.user = @user
@entry.save

每次都是同样的错误。

这是工厂文件:

  FactoryGirl.define do
    factory :user do
      name 'Test User'
      email 'example@example.com'
      password 'please'
      password_confirmation 'please'
      pin '1234'
      default_time_zone 'America/New_York'
    end
  ...

我正在使用设计,这是我们运行的控制器宏:

  module ControllerMacros

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = FactoryGirl.create(:user)
      #user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in @user
    end
  end

  end

这是完整的堆栈跟踪:

> 1.9.3p194 :016 > user = User.all[0]
 => #<User _id: 503e9c807adf9d6551000001, _type: nil, email: "joe@email.com", encrypted_password: "$5a$10$Mt1kx/TtbDma4gVtcTLdLeeP6tcSiF2AJkjeJmv73/U5OWF.M34v.", remember_created_at: 2012-08-29 23:30:17 UTC, sign_in_count: 3, current_sign_in_at: 2012-08-29 23:30:17 UTC, last_sign_in_at: 2012-08-29 22:49:57 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", s3_access_key: #<Moped::BSON::Binary type=:generic length=24>, s3_secret_key: #<Moped::BSON::Binary type=:generic length=48>, pin: nil, default_time_zone: "America/New_York", name: "Joe Smith"> 
1.9.3p194 :017 > e = FactoryGirl.create(("entry_image").to_s, user_id: user.id, items: [ FactoryGirl.build(:image, user_id: user.id) ])
TypeError: can't convert String into Integer
    from /Users/lee/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/set.rb:185:in `include?'
    from /Users/lee/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/set.rb:185:in `include?'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:140:in `block (3 levels) in cascadable_children'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:139:in `each'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:139:in `block (2 levels) in cascadable_children'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/relations/accessors.rb:103:in `without_autobuild'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:133:in `block in cascadable_children'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:131:in `each_pair'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:131:in `cascadable_children'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/callbacks.rb:95:in `run_callbacks'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.0/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.0/lib/active_model/validations.rb:179:in `valid?'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/validations.rb:78:in `valid?'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.0/lib/active_model/validations.rb:187:in `invalid?'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/persistence/insertion.rb:22:in `prepare'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.5/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
... 4 levels...
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/evaluation.rb:15:in `[]'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/evaluation.rb:15:in `create'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/strategy/create.rb:9:in `tap'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/strategy/create.rb:9:in `result'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/factory.rb:42:in `run'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.0/lib/active_support/notifications.rb:125:in `instrument'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/factory_runner.rb:22:in `run'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.0.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
    from (irb):17
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
    from /Users/lee/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
4

0 回答 0