我能得到我的代码一些帮助,这是抛出ActiveRecord::AssociationTypeMistach
我JobsController#创建。
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_category.rb模型和job_type.rb协会,了解更多信息张贴从我的表单中的数据为我job.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)你可以创建一个表job_categories
从job_category_id
表中删除job_category
和jobs
。 2)简单的选择,和一个我可能会建议您根据其他比你似乎是相对较新的数据库设计和Rails知之甚少,是完全删除的JobCategory
类,并摆脱belongs_to: job_category
的。您也可以从job_category_id
表中删除jobs
。如果你这样做,我们的代码的其余部分看起来应该是相当接近,只是工作。
你已经设置了关联和额外的课程真的意味着你到哪儿去存储在单独的表的其他数据的情况下,你是在告诉ActiveRecord的数据如何关联。这里,因为工作只有一个范畴,它实际上只是一个字符串相关的更多数据,把一切在一个表中是个不错的选择。