Rails 3:在父对象之一上创建子对象时更新多个父对象
我是 Rails 新手,现在正在尝试学习,希望有人能提供帮助。
我有 3 个具有一对多关系的用户、意见和投票模型。每个用户 has_many :opinions
和 has_many :votes
。每个意见都有_many :votes
和belongs_to :user
。每个投票属于用户
和属于意见
。投票表包含 :decision (boolean)、:opinion_id 和 :user_id 列。意见表只有:内容列。
我的用例是,用户添加意见,然后其他用户在查看每个意见(显示视图)时可以同意或不同意它。
在意见显示视图中,我想要两个带有“同意”和“不同意”的提交按钮。当用户提交投票时,我需要创建此投票(正确或错误)并更新投票表的 user_id 和 Opinion_id 字段。我成功地为每位家长单独做到了这一点,但没有为同一张投票同时做到这一点。任何帮助将不胜感激。
I am new to rails and trying to learn now so hopefully someone can help.
I have 3 models for User, Opinion and Vote with one-to-many relationships. Each user has_many :opinions
and has_many :votes
. Each opinion has_many :votes
and belongs_to :user
. Each vote belongs_to user
and belongs_to opinion
. Votes table has columns for :decision (boolean), :opinion_id and :user_id. Opinions table only has :content column.
My use case is that a user adds an opinion and then other users can either agree or disagree with it when viewing each opinion (show view).
In Opinion show view I want to have two submit buttons with "Agree" and "Disagree". When a user submits a vote I need to create this vote (true or false) and update both user_id and opinion_id fields of the votes table. I managed to do it for each parent individually but not both for the same vote. Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将两个 id 包含为隐藏字段。
意见显示视图:
投票控制器:
Include both ids as hidden fields.
Opinion show view:
Vote Controller: