我需要额外的关联吗?(Do I need an extra association?)

编程入门 行业动态 更新时间:2024-10-19 18:17:08
我需要额外的关联吗?(Do I need an extra association?)

我正在构建一个Rails应用程序,允许一个用户请求组的支持。

Class User has_many :requests Class Request belongs_to :users

目前,用户单击不同的链接以将请求发送到不同的子组。 我想通过让用户选择/取消选择人来允许进一步的自定义。

我是否需要一个协会来实现这一目标? 就像是...

Class User has_many :sent_requests, class_name 'Request' has_many :received_requests has_many :requests, :through received_requests Class Request belongs_to :client, class_name 'User' has_many :received_requests Class ReceivedRequest belongs_to :user belongs_to :client

这似乎是屁股的痛苦。 我可以 - 在请求#new控制器操作中创建@users实例变量,没有任何关联 - 将其传递给视图,使表单显示复选框 - 让用户取消选中人员 - 以某种方式将该变量传递回创建操作

我想更普遍的问题是,我如何决定是否需要添加关联?

最后,这可能是我需要使用嵌套资源的情况吗?

谢谢你的帮助。 我是新来的......

I'm building a Rails app to allow one user to request support from a group.

Class User has_many :requests Class Request belongs_to :users

Currently, the user clicks on different links to send the request to a different subgroup. I'd like to allow further customization by letting the user select/deselect people.

Do I need an association to make this happen? Something like...

Class User has_many :sent_requests, class_name 'Request' has_many :received_requests has_many :requests, :through received_requests Class Request belongs_to :client, class_name 'User' has_many :received_requests Class ReceivedRequest belongs_to :user belongs_to :client

This seems like a pain in the ass. Can I just - create a @users instance variable in the request#new controller action, without any association - pass it to the view, have the form display checkboxes - have the user uncheck people - somehow pass that variable back to the create action

I guess the more general question is, how do I decide I need to add an association?

Finally, could this be a case where I need to use nested resources?

Thanks for your help. I'm new at this...

最满意答案

您应该在此处使用关联,因为您要存储哪些用户与数据库中的特定请求相关联。 关联是与rails存储这些关系的正确方法。

如果设置正确,关联实际上非常容易管理。 在这种情况下,我认为使用嵌套资源没有任何优势。

另一种方法是仅使用修改后的HABTM关联,但在连接模型中包含一个属性,以指示哪个用户拥有该请求以及哪个用户正在接收该请求。

Class User has_many :requests, through: :user_requests has_many :user_requests Class Request has_many :users, through: :user_requests has_many :user_requests Class UserRequest belongs_to :user belongs_to :request

在UserRequest中有一个名为:owner的属性。 对于发出请求的用户和Request对象之间的Join,将其设置为true。 与该请求关联的其余用户将接收该请求。

You should use an association here since you'll want to store which users are linked with specific requests in the database. The association is the proper way to store these kinds of relationships with rails.

Associations are actually very easy to manage if they're set up properly. I don't see any advantage in using nested resources in this case.

On alternative would be to just use a modified HABTM association but include an attribute in the join model to indicate which user owns the request and which user is receiving it.

Class User has_many :requests, through: :user_requests has_many :user_requests Class Request has_many :users, through: :user_requests has_many :user_requests Class UserRequest belongs_to :user belongs_to :request

In UserRequest have a an attribute called :owner. Set this to true for the Join between the User issuing the request and the Request object. The remainder of the Users associated with that request will be receiving it.

更多推荐

本文发布于:2023-04-29 03:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:extra   association

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!