Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3754990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:37:46+00:00 2026-05-19T09:37:46+00:00

I’m having a little difficulty groking the call back model and methodology in Factory

  • 0

I’m having a little difficulty groking the call back model and methodology in Factory Girl. Using this post http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl I started giving it a go and, now I’m erroring in the spec. I am new to Rails…so I may be missing something really fundamental.

The Error is:
Failure/Error: @user = Factory(:admin_user)
undefined method ‘each’ for #

The Models:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable, :lockable,
         :recoverable, :rememberable, :trackable, :validatable, :token_authenticatble

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email, :role_ids, :password, :password_confirmation, :remember_me
  has_many :userroles
  has_many :roles, :through=>:userroles


  def role?(role)
    return !!self.roles.find_by_name(role.to_s.camelize)
  end
end

class Role < ActiveRecord::Base
  attr_accessible :name

  validates :name, :presence=>true,
                   :length=>{:minimum=>4, :maximum=>30}

  has_many :userroles
  has_many :users, :through=>:userroles
end

class Userrole < ActiveRecord::Base
  attr_accessible :user_id, :role_id
  belongs_to :user
  belongs_to :role
end

The Factories:

Factory.define :user do |f|
  f.email 'someone@somecompany.com'
  f.username 'foo'
  f.password '123456'
end

Factory.define :role do |r|
  r.name 'testrole'
end

Factory.define :userrole do |ur|
  ur.association(:role)
  ur.userrole{|ur| ur.userrole(:user)}
end
Factory.define :admin_role, :class => 'Role' do |userrole|
  userrole.role { |role| role.association(:role, :name => "Admin") }
end

Factory.define :admin_user, :parent => :user do |user|
  user.after_create { |u| Factory(:admin_role, :userroles => u) }
end

Lastly the simple spec:

require File.dirname(__FILE__) + '/../spec_helper'

describe RolesController do
  fixtures :all
  include Devise::TestHelpers
  render_views

before (:each) do
    @user = Factory(:admin_user)
    sign_in @user
  end

  it "index action should render index template" do
    get :index
    response.should render_template(:index)
  end
end

Update: Adding Stack Trace

ruby-1.9.2-p136 :001 > u=Factory(:admin_user)
NoMethodError: undefined method `each' for #<User:0x00000004d737d8>
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:352:in `replace'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/associations.rb:1524:in `block in collection_accessor_methods'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/build.rb:13:in `set'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `block in run'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `each'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `run'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
    from /home/ryan/Sites/Holocron/spec/factories/users.rb:22:in `block (2 levels) in <top (required)>'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `call'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `block in run_callbacks'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `each'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `run_callbacks'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/create.rb:7:in `result'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:327:in `run'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
    from (irb):1
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
    from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:37:47+00:00Added an answer on May 19, 2026 at 9:37 am

    I believe you’ve confused yourself.

    You’ve defined Factory.define :admin_role, :class => 'Role' do |userrole| as a Role but then in your factory definition you treat it like a Userrole. (BTW, Ruby conventions use underlines – so Userrole would be UserRole/user_role.)

    Also, your user.after_create { |u| Factory(:admin_role, :userroles => u) } probably ought to be: user.after_create { |u| Factory(:admin_role, :userroles => [u]) } since a Role has_many :userroles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.