2017-02-23 1 views
0

4 명의 사용자로 데이터베이스를 시드하려고합니다. 각 사용자에게는 has_one 프로필과 has_many todo_lists가 있습니다. 사용자 모델 : LNAME을 :시드 데이터베이스 레일 belongs_to

class User < ActiveRecord::Base 
    has_one :profile, dependent: :destroy 
    has_many :todo_lists, dependent: :destroy 
    has_many :todo_items, through: :todo_lists, source: :todo_items 
end 

여기 내 시드 파일

User.destroy_all 

user_list = [ 
    ["Carly", "Fiorina", "female", 1954], 
    ["Donald", "Trump", "male", 1946], 
    ["Ben", "Carson", "male", 1951], 
    ["Hillary", "Clinton", "female", 1947] 
] 

user_list.each do |fname, lname, gender, byear| 
    { 
     User.create!(username: lname, password_digest: "xyz") 
     User.profile.create! (first_name: fname, last_name: lname, gender: gender, birth_year: byear) 
     User.todo_list.create!(list_name: "temp", list_due_date: 1.year.from_now); 
    } 
end 

나는 오류가 예기치 않은 tLABEL이 이름을 가리키는 얻고있다. 사용자 테이블을 생성하기위한 이전 작업은 다음과 같습니다.

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :username 
     t.string :password_digest 

     t.timestamps null: false 
    end 
    end 
end 

답변

1

do-end 블록에는 대괄호를 사용하지 마십시오. 이것을 시도하십시오 :

user_list.each do |fname, lname, gender, byear| 
    user = User.create!(username: lname, password_digest: "xyz") 
    user.create_profile!(first_name: fname, last_name: lname, gender: gender, birth_year: byear) 
    user.todo_lists.create!(list_name: "temp", list_due_date: 1.year.from_now); 
end 
+0

예, 이것은 캐치 덕분입니다. 문제는 내가 사용자를 통해 프로필을 만들 수 없다는 것입니다. 어쩌면 프로파일을 작성한 다음 사용자에게 할당해야합니까? – leenyburger

+0

사용자 클래스가 아닌 사용자 인스턴스에서 프로필 및 todo_lists를 만들어야합니다. 이를 반영하여 답변을 업데이트했습니다. 프로필 및 TodoList 모델에는 외부 키가 있어야 사용자에게 다시 연결할 수 있습니다. – eiko

+0

이제 NoMethodError 정의되지 않은 메서드를 만듭니다! – leenyburger