Ruby on Rails:如何建模“用户最喜欢的模型”
我将使用 StackOverflow 作为示例。假设我有一个 Question
模型。登录用户可以为问题
“加注星标”,将其标记为他们最喜欢的问题之一。在数据库中,此类内容可能存储在带有 user_id
字段和 question_id
字段的 UserQuestions
表中。这种功能不是典型的 CRUD,因为实际上只有“列表”、“添加”和“删除”。此外,“用户加星标的问题”列表中显示的记录不应是 UserQuestion
记录,而是 Question
记录。我应该在控制器和 UserQuestion 模型中放入哪些代码?
class MyFavoriteQuestionsController < ApplicationController
def index
#list just the questions associated with current_user
end
def add
#insert a row in the database for current_user and selected question
def
def remove
#remove the row from the database
end
end
I'll use StackOverflow as my example. Let's say I have a Question
model. A logged in user can "star" a Question
to mark is as one of their favorites. In the database, this sort of thing would probably be stored in a UserQuestions
table with a user_id
field and a question_id
field. This sort of feature is not typical CRUD since there is really only "list", "add", and "delete". Also the records being displayed on the "User starred questions" list should be not UserQuestion
records but instead Question
records. What code do I put in my controller and UserQuestion
model?
class MyFavoriteQuestionsController < ApplicationController
def index
#list just the questions associated with current_user
end
def add
#insert a row in the database for current_user and selected question
def
def remove
#remove the row from the database
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你坚持惯例的话,我会说这是典型的粗俗。添加是创建,删除是销毁。
I'd say this is typical crud if you stick with convention. Add is create, remove is destroy.