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 8248967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:22:28+00:00 2026-06-07T23:22:28+00:00

I have a callback in my user.rb model something like: class User include Mongoid::Document

  • 0

I have a callback in my user.rb model something like:

class User
  include Mongoid::Document
  include Mongoid::Timestamps::Created
   .
   .
   .
   .
    #add a first board for user
    after_create :first_board
      def first_board
        board = self.boards.new(:title => self.username, :category_id => Category.find_or_create_by(slug: "other"))
        board.save!
      end
end

When a user is created, the app create a new board with above callback.

In my spec/factories/users.rb I have:

FactoryGirl.define do
  factory :user do
    name 'Test User'
    username 'test username'
    password 'please'
    password_confirmation 'please'
    # required if the Devise Confirmable module is used
    confirmed_at Time.now
  end
end

In my spec/controllers/users_controller_spec.rb I have:

require 'spec_helper'

describe UsersController do
  before :each do
    request.env['devise.mapping'] = Devise.mappings[:user]
  end
  include Devise::TestHelpers
  before (:each) do
    @user = FactoryGirl.create(:user)
    sign_in @user
  end

  describe "GET 'show'" do

    it "should be successful" do
      get :show, :id => @user.id
      response.should be_success
    end

    it "should find the right user" do
      get :show, :id => @user.slug
      assigns(:user).should == @user
    end

  end

end

How can I run my callback with factorygirl?

I get 2 errors:

Failures:

  1) UsersController GET 'show' should be successful
     Failure/Error: @user = FactoryGirl.create(:user)
     Errno::ECONNREFUSED:
       Connection refused - connect(2)
     # (eval):2:in `post'
     # ./app/models/user.rb:190:in `first_board'
     # ./spec/controllers/users_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

  2) UsersController GET 'show' should find the right user
     Failure/Error: @user = FactoryGirl.create(:user)
     Errno::ECONNREFUSED:
       Connection refused - connect(2)
     # (eval):2:in `post'
     # ./app/models/user.rb:190:in `first_board'
     # ./spec/controllers/users_controller_spec.rb:10:in `block (2 levels) in <top (required)>'
  • 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-06-07T23:22:29+00:00Added an answer on June 7, 2026 at 11:22 pm

    The find_or_create_by method can’t be called like that, afaik. It uses method_missing and expects an activerecord attribute tacked on the end. Try:

    Category.find_or_create_by_slug("other")

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

Sidebar

Related Questions

I have got this model: class User < ActiveRecord::Base has_and_belongs_to_many :roles before_destroy do |u|
I have a model Vote , and I'm using an after_validation callback. class Vote
I want to have one callback function after actions are done, I'm trying something
I'm writing some callback implementation in C++. I have an abstract callback class, let's
I have an Android class which has a callback from an asynchronous HTTP process,
I have a rails model User that has name , email and hash fields.
In my application I have an extended User model called UserProfile. This user can
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a rails app that is using Devise, with a User model, no
I have a model where everything is associated somehow to a single user (e.g.

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.