Ruby on Rails:两种模型之间的关系 [英] Ruby on Rails: Relation between two models
问题描述
我正在挑战自己建立一个小的市场,以便您可以在类别中发布请求".为此,我既有请求模型又有类别模型.如何在这些模型之间添加关系,以使Category知道它属于请求,反之亦然?我已经做到了:
I am challenging myself to build a little marketplace where you will be able to post a "request" inside a category. For that I have both the request model and the category model. How can I add a relation between those models, so that the Category knows that it belongs to a request and vice versa? I already did:
category.rb
has_and_belongs_to_many :requests
request.rb
has_one :category
现在在我的表单部分中,我有以下代码:
Now inside my form partial I have this code:
<%= f.select :category, Category.all, :prompt => "Kategorie", class: "form-control" %>
奇怪的是,:category
不存在,因为该列应为:name
.在我的seeds.rb
中,插入了以下内容,该内容在rake db:seed
The strange thing is :category
does not exist, as the column should be :name
. In my seeds.rb
I've inserted the following, which runs fine after rake db:seed
Category.create(name: 'PHP')
Category.create(name: 'Ruby')
Category.create(name: 'HTML')
Category.create(name: 'ASP')
Category.create(name: 'C#')
Category.create(name: 'C++')
但是上面带有:category
的代码显示了这一点:
But the above code with :category
shows this:
种子文件中共有6个类别,但没有类别的实际名称(例如"PHP").如果我在这段代码中使用:name
而不是:category
:
There are all 6 Categories from the seed file, but not the actual name of the category (like "PHP"). If i'm taking :name
instead of :category
in this code:
<%= f.select :category, Category.all, :prompt => "Kategorie", class: "form-control" %>
我得到
undefined method `name' for #<Request:0x007ff504266b40>
我的类别表:
Category(id: integer, name: string, description: text, created_at: datetime, updated_at: datetime)
保存后,如何命名特定请求的类别? @Category.request
?
How can I call the category for a specific request, when it's saved? @Category.request
?
我真的很困惑(对不起,我仅从8月下旬开始学习Rails).
I'm really confused (sorry I'm learning Rails only since late August).
非常感谢!
推荐答案
如果我正确理解它,例如在一个请求属于一个类别,而一个类别可以有多个请求,则应该这样建立关联:
If I understand it correctly, as in one Request belongs to one category and one category can have multiple requests the associations should be set up like this:
class Request < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :requests
end
像这样,请求表中的条目将具有类别的外键category_id.
Like this the entries in request table will have the foreign key category_id to the category.
您还可以在活动记录关联指南
You can also read a lot about association basics in the Active Record Associations Guide
保存后,如何命名特定请求的类别? @ Category.request吗?
How can I call the category for a specific request, when it's saved? @Category.request ?
要获取特定请求的类别,您必须从该请求开始,例如:
To get the category for a specifc request you have to start from the request for example like this:
@request = Request.first
@reqest.category
在表单中,如果要使用select标签,则可能必须使用category_id
:
In your form you probably then have to use category_id
if you want to use the select tag like this:
<%= f.select :category_id, Category.all.map { |c| [c.name, c.id] }, :prompt => "Kategorie", class: "form-control" %>
地图将确保您选择的地图将使用标签的名称和ID的值.
The map will make sure that it will use the name for the label and the id for the value in your select.
要使关联和其他内容的生成表单更容易,您还可以查看gem simple_form .然后,您只需使用:
To make generating forms for associations and other stuff easier you can also have a look at the gem simple_form. Then all you have to use is:
<%= f.association :category %>
这篇关于Ruby on Rails:两种模型之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!