播种数据库时不调用控制器助手方法

时间:2017-05-02 06:42:59

标签: ruby-on-rails

我有三个对象 - 用户,个人资料,匹配。

用户有一个个人资料。

  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")

1 个答案:

答案 0 :(得分:2)

创建记录时,不会调用

controllers create方法。这是模型的创建,称为

您可能希望将代码移至after_create回调

class User
  after_create :create_matches

  private

  def create_matches
    # your logic here
  end
end