本文介绍了factorygirl创建模型关联NoMethodError:未定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行FactoryGirl.create(:job, :purchased)时,出现以下错误.我已经为此进行了很长时间的斗争,我相信我有一个多元化的问题.

When I try to run FactoryGirl.create(:job, :purchased) I get the following error. I have been battling this for a long time now and I believe I have a pluralization issue.

问题

模型

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

错误

工作特征

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

推荐答案

在您的trait中,您正在定义association :payment, factory: :payment工作有很多付款.

In your trait your are defining association :payment, factory: :paymentbut job has_many payments.

要工作,您的模型应为:

To work, your models should be:

class Job < ActiveRecord::Base
  belongs_to :payment
end

class Payment < ActiveRecord::Base
  has_many :jobs
end

如果要保持模型不变,并创建包含多个付款的工作特征,则需要执行以下操作:

If you want to keep your model as you have and create a trait with a job containing multiple payments, you need to do something like this:

如何在FactoryGirl中建立工厂与has_many关联

这篇关于factorygirl创建模型关联NoMethodError:未定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 13:37