我正在构建一个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 :usersCurrently, 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 :clientThis 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 :requestIn 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.
更多推荐
发布评论