FactoryGirl - can't write unknown attribute, and undefined method `name'

I’m trying to create a photo factory that’s in a many to one
association
with gallery. And I’m getting two errors, depending on how I specify
the
association between those two models.

Here’s the models:

Photo

belongs_to :gallery, class_name: “SevenGallery::Gallery”

Gallery

has_many :photos, class_name: “SevenGallery::Photo”, foreign_key:
“seven_gallery_gallery_id”, dependent: :destroy

And the migrations

create_table :seven_gallery_galleries do |t|
t.string :title
t.timestamps null: false
end

create_table :seven_gallery_photos do |t|
   t.string :caption
   t.string :image
   t.references :seven_gallery_gallery, index: true
   t.timestamps null: false
 end


 add_foreign_key :seven_gallery_photos, :seven_gallery_galleries,

on_delete: :cascade

Now Here’s my factories:
Gallery:

FactoryGirl.define do
factory :gallery, class: ‘SevenGallery::Gallery’ do
title “an event gallery”
factory :gallery_with_photos do
after(:build) do |gallery|
gallery.photos << FactoryGirl.create(:photo_one,
seven_gallery_gallery_id: gallery)
gallery.photos << FactoryGirl.create(:photo_two,
seven_gallery_gallery_id: gallery)
gallery.photos << FactoryGirl.create(:photo_three,
seven_gallery_gallery_id: gallery)
end
end
end
end

And Photo:

FactoryGirl.define do
factory :photo, class: “SevenGallery::Photo” do

 factory :photo_one do
 end

 factory :photo_two do
 end

 factory :photo_three do
 end

 factory :photo_with_gallery do
  gallery
 end
  end
end

And here’s the controller spec that generates the error:

it “changes is_main to true in self and false in rest” do
photo_one = FactoryGirl.create(:photo_with_gallery)
expect(photo_one.gallery).to be_a SevenGallery::Gallery
end

Whenever I run the test I get this error:

Failure/Error: photo_one = FactoryGirl.create(:photo_with_gallery)
ActiveModel::MissingAttributeError:
can’t write unknown attribute gallery_id
# ./spec/controllers/seven_gallery/photos_controller_spec.rb:

When I change the association part to be:

factory :photo_with_gallery do
association :gallery, factory: gallery
end

I get this error:

Failure/Error: photo_one = FactoryGirl.create(:photo_with_gallery)
NoMethodError:
undefined method `name’ for :photo_with_gallery:Symbol
# ./spec/controllers/seven_gallery/photos_controller_spec.rb:8

Backtrace:

/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in
with_value_from_database' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:inwrite_from_user’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in
write_attribute_with_type_cast' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:inwrite_attribute’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in
write_attribute' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in[]=’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in
replace_keys' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:inreplace’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in
writer' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:ingallery=’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in
public_send' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:inblock (2 levels) in object’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in
each' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:inblock in object’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in
tap' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:inobject’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:12:in
object' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:inresult’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in
run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:inblock in run’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:166:in
instrument' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:inrun’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in
block in define_singular_strategy_method' # ./spec/controllers/seven_gallery/photos_controller_spec.rb:8:inblock (3 levels) in module:SevenGallery
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in
instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:inblock in run’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in
block in with_around_and_singleton_context_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:inblock in with_around_example_hooks’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in
block in run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:614:inblock in run_around_example_hooks_for’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in
call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:incall’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-rails-3.2.1/lib/rspec/rails/example/controller_example_group.rb:179:in
block (2 levels) in <module:ControllerExampleGroup>' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:ininstance_exec’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in
instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:inexecute_with’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in
block (2 levels) in run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:incall’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in
call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-rails-3.2.1/lib/rspec/rails/adapters.rb:113:inblock (2 levels) in module:MinitestLifecycleAdapter
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in
instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:ininstance_exec’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in
execute_with' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:inblock (2 levels) in run_around_example_hooks_for’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in
call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:incall’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:617:in
run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:inrun’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in
with_around_example_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:inwith_around_and_singleton_context_hooks’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:174:in
run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:549:inblock in run_examples’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in
map' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:inrun_examples’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:512:in
run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:inblock in run’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in
map' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:inrun’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in
block (3 levels) in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:inmap’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in
block (2 levels) in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1526:inwith_suite_hooks’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:109:in
block in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/reporter.rb:62:inreport’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:108:in
run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:86:inrun’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in
run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:ininvoke’
#
/home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/exe/rspec:4:in
<top (required)>' # /home/rafael/.rbenv/versions/2.2.0/bin/rspec:23:inload’
# /home/rafael/.rbenv/versions/2.2.0/bin/rspec:23:in `’

Any help would ne appreciated. Thanks.

https://github.com/chrislerum/factory
I tried whipping up an app with your code, the controller test passes
fine
for me, see if it works for you and compare.

I can see the only difference is the namespace of the engine is used but
in
your case is not, since it’s a normal app. But is this the cause ?

I can see the only difference is that in my case there’s namespace in
front
of the table names and FK. But in yours there’s none since it’s a normal
app.
But is this the cause ?

In short, yes. See this lovely article; notice the conspicuous absence
of
any talk of changing table names:
http://blog.makandra.com/2014/12/organizing-large-rails-projects-with-namespaces/

Are you sure about this ? If so, is there a way to make it working
without
changing table names ?

you could try, in the model, self.table_name = “seven_gallery_photos”
etc.

On Thu, Apr 30, 2015 at 4:09 AM, Rafael A. [email protected]

https://github.com/chrislerum/factory setting table_name seems to be the
sweet spot, working now

This is really driving me insane. I’ve something very similar to your
sample Here https://github.com/rafaeladel/seven_event. I’ve specified
table names inside models. But still the error occurs. Would you mind
taking a look at it ? Thank you a lot so far for your time.

Not sure how useful this is, but I get it working if I change the table
names to ‘galleries’ and ‘photos’.
updated: https://github.com/chrislerum/factory

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs