
[英]Building an active record with associations to two belongs_to relationships
[英]Can “belongs_to” be defined more than once in Active_Record?
我正在尝试为镇上的一家公司编写数据库。 我正在使用Devise进行身份验证,并使用Forem进行网站的论坛。 我决定只为Devise身份验证提供一个类“ Account”,该类将具有对该站点的许多不同访问类型。
用户的大部分将只是按路线(而不是Rails路线,街道路线)分开的客户。 因此,我决定让他们拥有自己的个人资料模型。
我想这样做-配置文件链接到帐户,并路由。 (路由名为gmr_routes)
这段代码是正确的方法吗? 我找到的文档没有告诉我不能,但是我只是想确定...。
class Profile < ActiveRecord::Base
attr_accessible :first_name, :last_name, :phone_number, :street_address
belongs_to :account
belongs_to :gmr_route
end
帐户与配置文件具有has_one关系,而gmr_route具有has_many。
这是正确的吗?
布赖恩
是的,完全可以接受。 您需要记住,在任何带有belongs_to的模型上都包含外键ID。
因此,在您描述的情况下,您的迁移中将具有account_id:integer
和gmr_route_id:integer
,并将那些包含在模型的attr_accessible
调用中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.