Foros del Web » Programando para Internet » Ruby »

[SOLUCIONADO] Relaciones entre modelos no me dejan crear uno con FactoryGirl.build

Estas en el tema de Relaciones entre modelos no me dejan crear uno con FactoryGirl.build en el foro de Ruby en Foros del Web. Código: class User < ActiveRecord::Base has_many :events has_many :participants has_many :events, through: :participants end class Event < ActiveRecord::Base belongs_to :user belongs_to :event_status has_many :participants has_many ...
  #1 (permalink)  
Antiguo 09/11/2015, 15:26
 
Fecha de Ingreso: junio-2011
Ubicación: Caracas
Mensajes: 2
Antigüedad: 12 años, 10 meses
Puntos: 0
Exclamación Relaciones entre modelos no me dejan crear uno con FactoryGirl.build

Código:
class User < ActiveRecord::Base
  has_many :events
  has_many :participants
  has_many :events, through: :participants
end

class Event < ActiveRecord::Base
  belongs_to :user
  belongs_to :event_status
  has_many :participants
  has_many :user, through: :participants
end

class Participant < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
end

FactoryGirl.define do
    factory :event do
    	user
    	event_status
        name { FFaker::Name.name }
        description { FFaker::Lorem.phrase }
        avatar { FFaker::Avatar.image }
        latitude { FFaker::Geolocation.lat }
        longitude { FFaker::Geolocation.lng }
    end
end

FactoryGirl.define do
  factory :user do
    first_name { FFaker::Name.first_name }
    last_name { FFaker::Name.last_name }
    phone_number { FFaker::PhoneNumber.phone_number }
    email { FFaker::Internet.email }
    handle { FFaker::Name.name }
    password "12345678"
    imei "1234567890"
    gender "M"
  end
end

FactoryGirl.define do
  factory :event_status do
  	name { FFaker::Name.first_name }
  	description { FFaker::Lorem.phrase }
  end
end
Cuando trato de crear un modelo para mi prueba en la consola de rails

Código:
event = FactoryGirl.build(:event)
Me sale el siguiente error

Código:
NoMethodError: undefined method `each' for #<User:0x007fb6338b2a40>
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activemodel-4.2.3/lib/active_model/attribute_methods.rb:433:in `method_missing'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.3/lib/active_record/associations/collection_association.rb:365:in `replace'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.3/lib/active_record/associations/collection_association.rb:47:in `writer'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.3/lib/active_record/associations/builder/association.rb:123:in `user='
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `public_send'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:12:in `object'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/build.rb:9:in `result'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:166:in `instrument'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
	from /Users/german/.rbenv/versions/2.2.2/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'
	from (irb):2
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /Users/german/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'irb(main):003:0>
Creo que el problema esta con las relaciones, estoy empezando en RoR y si me pueden guiar les agradeceria muchisimo

Etiquetas: rails
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:26.