helpmeplz
helpmeplz

Reputation: 181

Controller helper method not being called when seeding database

I have three objects - User, Profile, Match.

User has one profile.

  create_table "matches", force: :cascade do |t|
    t.integer  "user_one_id"
    t.integer  "user_two_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.index ["user_one_id", "user_two_id"], name: "index_matches_on_user_one_id_and_user_two_id", unique: true, using: :btree
    t.index ["user_one_id"], name: "index_matches_on_user_one_id", using: :btree
    t.index ["user_two_id"], name: "index_matches_on_user_two_id", using: :btree
  end

  create_table "profiles", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "gender"
    t.string   "occupation"
    t.string   "religion"
    t.date     "date_of_birth"
    t.string   "picture"
    t.string   "smoke"
    t.string   "drink"
    t.text     "self_summary"
    t.integer  "user_id"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.index ["user_id"], name: "index_profiles_on_user_id", using: :btree
  end

  create_table "users", force: :cascade do |t|
    t.string   "email"
    t.datetime "created_at",                      null: false
    t.datetime "updated_at",                      null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",           default: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
  end

Once user registers, they create a profile. When Profile is created, Match objects are generated automatically via create_matches method.

# profiles_controller.rb

def create
    @profile = current_user.create_profile(profile_params)
    if @profile.save
      flash[:success] = "Welcome to MatchMe!"
      create_matches(@user)
      redirect_to @user
    else
      render 'new'
    end
end

# matches_helper.rb

module MatchesHelper

  def create_matches(user)
    users = User.all
    users.each do |u|
      # Check that user is not an admin or current user
      unless u.id == user.id || u.admin?
        Match.create!(user_one_id: user.id, user_two_id: u.id)
      end
    end
  end
end

This works when I manually create a new user and user profile via sign up form, but not when I seed the database with new users. It's as though the create_matches method is skipped.

Here is my seed file:

# seeds.rb

# Admin user
User.create!(email: "[email protected]",
        admin: true,
        password: "foobar1",
        password_confirmation: "foobar1")

# John user
john = User.create!(email: "[email protected]",
                    password: "foobar1",
                    password_confirmation: "foobar1")

# John profile
john.create_profile!(first_name: "John", 
                    last_name: "Doe",
                    gender: "male",
                    religion: "Other",
                    occupation: "Salesman",
                    date_of_birth: Date.new(1990,1,1),
                    smoke: "not at all",
                    drink: "socially")

# Naomi - Matches with John
naomi = User.create!(email: "[email protected]",
                    password: "foobar1",
                    password_confirmation: "foobar1")

# Naomi profile
naomi.create_profile!(first_name: "Naomi", 
                    last_name: "Smith",
                    gender: "female",
                    religion: "Atheist",
                    occupation: "student",
                    date_of_birth: Date.new(1992,1,1),
                    smoke: "not at all",
                    drink: "socially")

Upvotes: 0

Views: 33

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

controllers create method will not be called when you create the records. It's the model's create which is called

You might want to move the code to after_create callback

class User
  after_create :create_matches

  private

  def create_matches
    # your logic here
  end
end

Upvotes: 2

Related Questions