0

工場の扱い方とattr_accessible

私の例:

# model
class SomeModel

  attr_accessible :name, full_name, other_name

end

#spec
require 'spec_helper'

describe "test" do
  it do
    create(:some_model, name: "test name", user: User.first) #factory
  end

end

#error
ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

エラーは属性にuser_idないためだと思います。attr_accessible

4

3 に答える 3

2

わかりましたが、とにかく、アソシエーションを使用してファクトリを定義する場合は、attr_protected

factory :some_model do |sm|

  sm.name "John"
  sm.full_name "John Smith"
  sm.other_name  "some other"

  sm.association :user, :factory => :user
end

そしてより

describe "test" do
  it "should create models with associations" do
    Factory(:some_model, name: "test name", user: User.first) #factory
  end
end
于 2012-06-25T17:41:03.253 に答える
0

これは、attr_accessibleが原因で取得したものとは異なるようです。ただし、 attr_accessibleに:userを追加するだけです。

于 2012-06-25T12:10:18.477 に答える
0

あなたはそれを必要としませんSomeModelか?

belongs_to :user
于 2012-06-25T14:20:05.050 に答える