Ruby on Rails:如何建模“用户最喜欢的模型”

发布于 2024-11-29 09:25:13 字数 679 浏览 1 评论 0原文

我将使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2024-12-06 09:25:13

如果你坚持惯例的话,我会说这是典型的粗俗。添加是创建,删除是销毁。

class FavouritesController < ApplicationController

  before_filter :find_user

  def index
    @favourites = @user.favourites
  end

  def create
    @question = Question.find params[:id]
    @user.favourites << @question
  def

  def destroy
    @favourite = @user.favourites.find_by_question_id params[:id]
    @favourite.destroy unless @favourite.blank?
  end
end


#routes.rb

resources :users do
  resources :favourites, :only => [:index, :create, :destroy]
end

#user.rb

has_many :user_favourites, :dependent => :destroy
has_many :favourites, :through => :user_favourites, :source => :question

I'd say this is typical crud if you stick with convention. Add is create, remove is destroy.

class FavouritesController < ApplicationController

  before_filter :find_user

  def index
    @favourites = @user.favourites
  end

  def create
    @question = Question.find params[:id]
    @user.favourites << @question
  def

  def destroy
    @favourite = @user.favourites.find_by_question_id params[:id]
    @favourite.destroy unless @favourite.blank?
  end
end


#routes.rb

resources :users do
  resources :favourites, :only => [:index, :create, :destroy]
end

#user.rb

has_many :user_favourites, :dependent => :destroy
has_many :favourites, :through => :user_favourites, :source => :question
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文