开发者

Rails Associations with a Single Owner and Multiple Responders

How would I use Rails Associations to model the following:

  • A user can make any number of "Request"s. The "Request" then has the user as the owner
  • The system would then assign a small number of other users to the requests as potential responders
  • Eventually, one of the responders can actually respond, at which point, that responder will be marked as "The Responder" of the "Request"

Sorry if this is a little vague. If you guys need more elaboration, feel free to ask!

I was thinking about so开发者_开发百科mething like this, but obviously its not allowed. Is there a way to fix this?

User has_many Requests, Responses

Request belongs_to User
Responses belongs_to User

Request has_many Responses
Responses belongs_to Request


So to say, there are 3 models: User, Request, Response. User has_many request and response, and Request and Response each belongs_to user.

In the view of each user, you can list the requests or responses separately to your liking. and in view showing any request, you can list the user of each response like

<h2>Response from <%= @response.user.username %></h2>

The official rails guide can give you an idea of the code, since yours will be easier, but feel free to ask if in doubt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜