2017-05-02 5 views
0

私は3つのオブジェクト(User、Profile、Match)を持っています。データベースをシードするときにコントローラヘルパーメソッドが呼び出されない

ユーザーは1つのプロファイルを持っています。

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 

ユーザーが登録すると、プロファイルが作成されます。プロファイルが作成されると、Matchオブジェクトは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: "[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") 

答えて

2

コントローラcreateメソッドが呼び出されることはありません。

は、ここに私のシードファイルです。それは

あなたはafter_createコールバックにコードを移動することができますと呼ばれているのは、作成したモデルだ

class User 
    after_create :create_matches 

    private 

    def create_matches 
    # your logic here 
    end 
end 
関連する問題