播种数据库时未调用控制器辅助方法



我有三个对象 - 用户,配置文件,匹配。

用户有一个配置文件。

  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

一旦用户注册,他们就会创建一个配置文件。创建配置文件时,匹配对象将通过create_matches方法自动生成。

# 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

当我通过注册表单手动创建新的用户和用户配置文件时,这起作用,而当我与新用户播种数据库时"。好像是跳过了create_matches方法。

这是我的种子文件:

# seeds.rb
# Admin user
User.create!(email: "admin@example.com",
        admin: true,
        password: "foobar1",
        password_confirmation: "foobar1")
# John user
john = User.create!(email: "joedoe@example.com",
                    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: "naomi@example.com",
                    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")

当您创建记录时,create方法将不会调用。这是模型的创建,称为

您可能需要将代码移至after_create回调

class User
  after_create :create_matches
  private
  def create_matches
    # your logic here
  end
end

最新更新