HABTM 2 个表 2 个不同的关系
我有一个服务类型表,其中包含几十个服务的 ID 和名称。
我有一个项目表,其中必须包含建议服务列表和已接受服务列表。
我知道我会在两侧使用 HABTM,并在其间使用 project_service_types 表。
当同一张表之间有两个不同的关系时,我不太清楚该怎么做。 我怀疑它使用 :join_table 和 :linked_forign_key,但我无法让它在我的应用程序中工作。
谢谢。
I have a Service Types table containing id and name of a couple of dozen services.
I have a Projects table that has to have a list of Proposed Services, and a list of Accepted Services.
I know that I would use HABTM on both sides with a project_service_types table in between.
I can't quite figure out what to do when I have 2 different relationships between the same table. I suspect it uses the :join_table and :associated_forign_key, but I can't get it to work in my app.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我用HABTM解决了这个问题...
I solved it using HABTM...
虽然您可以使用 habtm 解决此问题,但您所讨论的是 has_many :through 的用例。 您想要在关系中附加一些信息。 为此,您需要创建一个表示关系的连接模型。
最后,这使您可以将您的服务提案视为您所在领域中的一流“事物”。 当服务被接受后,您只需更改状态即可。 这也节省了连接。
迁移
模型
While you can solve this with habtm, what you're talking about is the use-case for has_many :through. You want to attach a bit of information along with the relationship. To do this you create a join model that represents the relationship.
In the end this allows you to treat your service proposal as a first-class "thing" in your domain. When the service is accepted you can just change the status. This also saves a join.
Migration
Models
为此,您可能需要使用 has_many :through ,如下所示:
此处的多对多部分:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
更详细地解释了这一点。 希望这可以帮助!
For that you'd probably want to use has_many :through as in:
The many-to-many section here:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
explains this in more detail. Hope this helps!