我项目中Controller#create中的ActiveRecord :: AssociationTypeMismatch错误

时间:2019-02-03 14:39:05

标签: ruby-on-rails ruby ruby-on-rails-5

我可以在我的JobsController#create中抛出ActiveRecord::AssociationTypeMistach的代码获得帮助吗?

ActiveRecord::AssociationTypeMismatch (JobCategory(#70843392) expected, got "At Home" which is an instance of String(#20478408)):    
app/controllers/jobs_controller.rb:46:in `create'

我正在通过表单

发布数据
<%= form_for @job do |f| %>
  <div class="row">
    <div class="col-md-4 select">
      <div class="form-group">
        <label>Job Category</label>
        <%= f.collection_select :job_category, JobCategory.order(:job_category),
          :job_category, :job_category, include_blank: false, id: "job_category",
          prompt: "Select...", class: "form-control" %>
      </div>
    </div>

    <div class="col-md-4 select">
      <div class="form-group">
        <label>Job Type</label>
        <%= f.grouped_collection_select :job_type, JobCategory.order(:job_category),
          :job_types, :job_category, :job_type_id, :job_type, id: "job_type",
          prompt: "Select...", class: "form-control"    %>
      </div>
    </div>

    <div class="col-md-4 select">
      <div class="form-group">
        <label>Frequency</label>
        <%= f.select :recurrence, [["One Off", "One Off"], ["Daily", "Daily"],
          ["Weekly", "Weekly"], ["Bi-Monthly", "Bi-Monthly"],
          ["Once-Monthly", "Once-Monthly"]],
          id: "recurrence", prompt: "Select...", class: "form-control" %>
      </div>
    </div>
  </div>

  <div><%= f.submit "Post My Job", class: "btn btn-normal" %></div>
<% end %>

作业模型的架构

  create_table "jobs", primary_key: "job_id", force: :cascade do |t|
    t.string "job_title"
    t.text "job_description"
    t.text "key_instructions"
    t.integer "budget"
    t.datetime "booking_date"
    t.string "recurrence"
    t.boolean "is_flexible"
    t.string "address"
    t.boolean "active"
    t.integer "user_id"
    t.datetime "date_posted"
    t.datetime "date_ending"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "featured"
    t.boolean "premium"
    t.integer "job_category_id"
    t.integer "job_type_id"
    t.string "job_category"
    t.string "job_type"
    t.index ["job_category_id"], name: "index_jobs_on_job_category_id"
    t.index ["job_type_id"], name: "index_jobs_on_job_type_id"
    t.index ["user_id"], name: "index_jobs_on_user_id"
  end

我将表单中的数据发布到job.rb模型中,但是在此过程中,使用job_category.rb模型和job_type.rb关联获取其他信息。这三个模型的相关性如下

class Job < ApplicationRecord
  belongs_to :user
  belongs_to :job_category
  belongs_to :job_type
  has_many_attached :images

  validates :job_category, presence: true
  validates :job_type, presence: true
  validates :recurrence, presence: true    
end

class JobType < ApplicationRecord
  has_many :job_categories
  has_many :jobs

  #attr_accessible :job_type
  #validates :job_type, :presence => true
end

class JobCategory < ApplicationRecord
  has_many :job_types
  has_many :jobs

  #attr_accessible :job_category
  #validates :job_category, :presence => true    
end

关于我要去哪里的任何提示都将很有用。谢谢

1 个答案:

答案 0 :(得分:0)

根据您的问题,您有两种职位类别选择(这也应适用于职位类型): 1)您可以创建表job_categories,然后从job_category_id表中删除job_categoryjobs。 2)一个更简单的选项是完全摆脱JobCategory类,并且摆脱掉{ belongs_to: job_category。您也可以从job_category_id表中删除jobs。如果这样做的话,我们其余的代码看起来就应该差不多可以正常工作了。

您设置的关联和额外的类实际上是用于将其他数据存储在单独的表中,并告诉ActiveRecord该数据如何关联的情况。在这里,由于作业只有一个类别,实际上只是一个字符串,没有关联的其他数据,因此将所有内容放在一个表中并不是一个坏选择。