1
DB를 채우는 레이크 작업을 만들려고합니다. 내가 deatails의 특정 세트와 99 기타 임의로 genraterated와 1 사용자를 만들 필요가있다. 필요한 세부 정보는 중첩 된 속성이있는 양식과 유사하게 두 개의 모델에 동시에 적용됩니다. 내 모델은 after_create 콜백을 사용하여 사용자를 생성하고 팀에 연결합니다. 기본적으로 팀 이름은 사용자가 내가 오류를레이크 작업의 중첩 된 속성
sample_data.rake 중첩 attibute에 relys가
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
user = User.create!(:profile_attributes => { name: Forgery::Name.full_name },
email: "[email protected]",
password: "123qwe",
password_confirmation: "123qwe")
99.times do |n|
:profile_attributes => { name: Forgery::Name.full_name },
email = Forgery::Internet.email_address
password = "password"
user = User.create!(email: email,
password: password,
password_confirmation: password)
end
end
end
모델을 생산 사용한
현재 코드 이름입니다.
class User < ActiveRecord::Base
after_create :set_user_on_team
private
def set_user_on_team
self.create_owned_team(:name => self.profile.name).users << self
end
end
오류 메시지
rake aborted!
/lib/tasks/sample_date.rake:9: syntax error, unexpected tASSOC, expecting keyword_end
:profile_attributes => { name: Forgery::Name.full_name },
^
/lib/tasks/sample_date.rake:9: syntax error, unexpected ',', expecting keyword_end
오류 메시지를 포함하도록 질문이 업데이트되었습니다. 포함 된 레이크 파일은 현재 시도입니다 –
아마도 9 행을 제거해야합니다 : profile_attributes => {name : Forgery :: Name.full_name},'accepts_nested_attributes_for : profile'을 추가하고 모델 관계'has_one : profile'을 선언하십시오. 또한 전체 응용 프로그램에서 일관된 해시 스타일 (이전 또는 신규)을 사용하는 것이 좋습니다. – taro
그들은 내 코드에 포함되어 있습니다. 콜백을 지우기 위해 제거했습니다. –