factorygirl创建模型关联NoMethodError:未定义的方法

时间:2016-03-19 21:53:30

标签: ruby-on-rails model factory-bot

当我尝试运行FactoryGirl.create(:job, :purchased)时,出现以下错误。我一直在争取这个很长一段时间,我相信我有一个多元化的问题。

问题

enter image description here

模型

class Job < ActiveRecord::Base
  belongs_to :company
  belongs_to :category
  has_one :coupon
  has_many :payments
end

class Payment < ActiveRecord::Base
  belongs_to :job
  belongs_to :coupon
end

class Coupon < ActiveRecord::Base
  belongs_to :job
end

工厂

FactoryGirl.define do
  factory :job do
    category
    company
    title { FFaker::Company.position }
    location { "#{FFaker::Address.city}, #{FFaker::AddressUS.state}" }
    language_list { [FFaker::Lorem.word] }
    short_description { FFaker::Lorem.sentence }
    description { FFaker::HTMLIpsum.body }
    application_process { "Please email #{FFaker::Internet.email} about the position." }

    trait :featured do |job|
      job.is_featured true
    end

    trait :reviewed do |job|
      job.reviewed_at { Time.now }
    end

    trait :purchased do |job|
      job.reviewed_at { Time.now }
      job.start_at { Time.now }
      job.end_at { AppConfig.product['settings']['job_active_for_day_num'].day.from_now }
      job.paid_at { Time.now }
      association :payment, factory: :payment
    end

    trait :expired do |job|
      start_at = (200..500).to_a.sample.days.ago
      job.reviewed_at { start_at }
      job.start_at { start_at }
      job.end_at { |j| j.start_at + AppConfig.product['settings']['job_active_for_day_num'].days }
      job.paid_at { start_at }
      # TBD ADD PAYMENT
    end
  end
end

部分架构

  create_table "payments", force: :cascade do |t|
    t.decimal  "price_paid",            precision: 8, scale: 2, default: 0.0
    t.string   "stripe_customer_token"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
  end

  create_table "jobs", force: :cascade do |t|
    t.string   "title",               limit: 50,                  null: false
    t.string   "slug",                limit: 250,                 null: false, index: {name: "index_jobs_on_slug"}
    t.string   "vanity_url",          limit: 250
    t.string   "location",            limit: 100,                 null: false
    t.string   "short_description",   limit: 250,                 null: false
    t.text     "description",         null: false
    t.text     "application_process", null: false
    t.boolean  "is_featured",         default: false
    t.datetime "start_at"
    t.datetime "end_at"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.integer  "company_id",          index: {name: "index_jobs_on_company_id"}, foreign_key: {references: "companies", name: "fk_jobs_company_id", on_update: :no_action, on_delete: :no_action}
    t.datetime "deleted_at",          index: {name: "index_jobs_on_deleted_at"}
    t.integer  "category_id",         index: {name: "index_jobs_on_category_id"}, foreign_key: {references: "categories", name: "fk_jobs_category_id", on_update: :no_action, on_delete: :no_action}
    t.datetime "paid_at"
    t.datetime "reviewed_at"
    t.integer  "payment_id",          index: {name: "index_jobs_on_payment_id"}, foreign_key: {references: "payments", name: "fk_jobs_payment_id", on_update: :no_action, on_delete: :no_action}
  end

  create_table "coupons", force: :cascade do |t|
    t.integer  "code",             limit: 8, null: false, index: {name: "index_coupons_on_code", unique: true}
    t.integer  "percent_discount", limit: 2, null: false
    t.datetime "start_at",         null: false
    t.datetime "end_at",           null: false
    t.datetime "executed_at"
    t.datetime "deleted_at",       index: {name: "index_coupons_on_deleted_at"}
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "job_id",           index: {name: "index_coupons_on_job_id"}, foreign_key: {references: "jobs", name: "fk_coupons_job_id", on_update: :no_action, on_delete: :no_action}
    t.integer  "payment_id",       index: {name: "index_coupons_on_payment_id"}, foreign_key: {references: "payments", name: "fk_coupons_payment_id", on_update: :no_action, on_delete: :no_action}
  end

下面的每次聊天更新

  create_table "jobs", force: :cascade do |t|
    t.string   "title",               limit: 50,                  null: false
    t.string   "slug",                limit: 250,                 null: false, index: {name: "index_jobs_on_slug"}
    t.string   "vanity_url",          limit: 250
    t.string   "location",            limit: 100,                 null: false
    t.string   "short_description",   limit: 250,                 null: false
    t.text     "description",         null: false
    t.text     "application_process", null: false
    t.boolean  "is_featured",         default: false
    t.datetime "start_at"
    t.datetime "end_at"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.integer  "company_id",          index: {name: "index_jobs_on_company_id"}, foreign_key: {references: "companies", name: "fk_jobs_company_id", on_update: :no_action, on_delete: :no_action}
    t.datetime "deleted_at",          index: {name: "index_jobs_on_deleted_at"}
    t.integer  "category_id",         index: {name: "index_jobs_on_category_id"}, foreign_key: {references: "categories", name: "fk_jobs_category_id", on_update: :no_action, on_delete: :no_action}
    t.datetime "paid_at"
    t.datetime "reviewed_at"
  end

  create_table "coupons", force: :cascade do |t|
    t.integer  "code",             limit: 8, null: false, index: {name: "index_coupons_on_code", unique: true}
    t.integer  "percent_discount", limit: 2, null: false
    t.datetime "start_at",         null: false
    t.datetime "end_at",           null: false
    t.datetime "executed_at"
    t.datetime "deleted_at",       index: {name: "index_coupons_on_deleted_at"}
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "job_id",           index: {name: "index_coupons_on_job_id"}, foreign_key: {references: "jobs", name: "fk_coupons_job_id", on_update: :no_action, on_delete: :no_action}
  end

  create_table "payments", force: :cascade do |t|
    t.decimal  "price_paid",            precision: 8, scale: 2, default: 0.0
    t.string   "stripe_customer_token"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
    t.integer  "job_id",                index: {name: "index_payments_on_job_id"}, foreign_key: {references: "jobs", name: "fk_payments_job_id", on_update: :no_action, on_delete: :no_action}
    t.integer  "coupon_id",             index: {name: "index_payments_on_coupon_id"}, foreign_key: {references: "coupons", name: "fk_payments_coupon_id", on_update: :no_action, on_delete: :no_action}
  end

错误 enter image description here

特色工作

trait :purchased do |job|
  job.reviewed_at { Time.now }
  job.start_at { Time.now }
  job.end_at { AppConfig.product['settings']['job_active_for_day_num'].day.from_now }
  job.paid_at { Time.now }
  payments { |j| [j.association(:payment)] }
end

1 个答案:

答案 0 :(得分:1)

trait中定义association :payment, factory: :payment工作有很多付款

要工作,你的模型应该是:

class Job < ActiveRecord::Base
  belongs_to :payment
end

class Payment < ActiveRecord::Base
  has_many :jobs
end

如果您希望保留模型,并创建包含多笔付款的工作的特征,则需要执行以下操作:

How to set up factory in FactoryGirl with has_many association