1

DBにデータを入力するためのrakeタスクを作成しようとしています。特定のdeatailsセットと99個の他のランダムに生成されたユーザーを1人作成する必要があります。必要な詳細は、ネストされた属性を持つフォームと同様に、同時に2つのモデルに適用されます。私のモデルは、after_createコールバックを使用して、ユーザーを作成し、チームにリンクします。デフォルトのチーム名はユーザー名です

私が使用した現在のコードはエラーを生成します

sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
  user = User.create!(:profile_attributes => { name: Forgery::Name.full_name },
              email: "test@example.com",
              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
4

1 に答える 1

1

99.timesブロックにいくつかの構文エラーがあることに気付きました。次のように試すことができます。

99.times do |n|
  profile_attributes = { name: Forgery::Name.full_name }
  email = Forgery::Internet.email_address
  password  = "password"
  user = User.create!(profile_attributes: profile_attributes, #my assumption
                      email: email,
                      password: password,
                      password_confirmation: password)
end 
于 2012-07-29T17:10:01.303 に答える