如何使用 has_many :through 关联更新连接模型?

发布于 2024-10-04 12:41:20 字数 1085 浏览 4 评论 0原文

我有一个关于多对多关系的问题,特别是 has_many :through 关联。

我发现的所有教程都只是为您设置了模型和迁移,但在涉及控制器时却让您感到困惑。

我想要做的是,当再次添加已经存在的文章时,更新连接表的时间戳,以便它移动到“列表”的顶部。我该怎么做?

这就是我的创建操作的样子:

def create
  @article = Article.find_or_create_by_url(params[:article])
  if current_user.articles.find(@article)

      # update the timestamps on the join table 
      # ie moved the old article to the top

      flash[:notice] = "Successfully added article (again)."
      redirect_to @article
  else
    @article.users << current_user
    if @article.save
      flash[:notice] = "Successfully added article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end
end

提前致谢!

更新:

@Ben Lee

感谢您的回答,因为我通过关联有一个 has_many 我的文章模型如下所示:

has_many :readinglist_items, :dependent => :destroy
has_many :users, :through => :readinglist_items

所以我不知道是否可以添加 :touch =>; true 为 has_many,因为我只想连接表中的特定条目。

创建操作中更新的要点是,如果用户添加了过去已添加的文章,则将文章移动到顶部(而不是再次添加)。

I have a question regarding many-to-many relationships, specifically has_many :through associations.

All the tutorials I found just set you up with the models and migrations but leave you hanging when it comes to controllers.

What I want to do is to update the timestamps of the join table when an article that is already present is added again, so that it moves to the top of the "list". How do I do that?

This is what my create action looks like:

def create
  @article = Article.find_or_create_by_url(params[:article])
  if current_user.articles.find(@article)

      # update the timestamps on the join table 
      # ie moved the old article to the top

      flash[:notice] = "Successfully added article (again)."
      redirect_to @article
  else
    @article.users << current_user
    if @article.save
      flash[:notice] = "Successfully added article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end
end

Thanks in advance!

Update:

@Ben Lee

Thanks for your answer, as I have a has_many through association my article model looks like this:

has_many :readinglist_items, :dependent => :destroy
has_many :users, :through => :readinglist_items

So I don't know if I can add a :touch => true to has_many as I just want to specific entry in the join table.

The point of the update in the create action is to move the article to the top (instead of adding it again) if a user adds an article it have already added in the past.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别忘他 2024-10-11 12:41:20

如果我理解正确的话,这不是控制器问题,而是模型问题。您可以指定 :touch => belongs_to 关系上为 true。这将使每当子项更新时,关联的 update_at 都会得到很好的更新。

因此,在您的 Article 模型中放入这样的内容:

belongs_to :whatever, :touch => true

另外,切线相关:从代码中不清楚您的代码到底在做什么,但看起来也许您正在放置 createupdate 功能都在 create 方法中,而不是适当地将它们分开。

If I'm understanding you correctly, this isn't a controller issue, but a model issue. You can specify :touch => true on a belongs_to relationship. This will make it so that whenever the child is updated, the association's update_at is updated well.

So put something like this in your Article model:

belongs_to :whatever, :touch => true

Also, tangentially related: it's not clear from the code exactly what your code is doing, but it seems like maybe you are putting create and update functionality both in in the create method instead of splitting them up appropriately.

荆棘i 2024-10-11 12:41:20

我解决了! (readinglist_item是连接表的名称):

def create
  @article = Article.find_or_create_by_url(params[:article])
  if current_user.articles.find(@article)

      @article.readinglist_items.find_by_user_id(current_user.id).touch

      flash[:notice] = "Successfully added article (again)."
      redirect_to @article
  else
    @article.users << current_user
    if @article.save
      flash[:notice] = "Successfully added article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end
end

I solved it! (readinglist_item is the name of the join table):

def create
  @article = Article.find_or_create_by_url(params[:article])
  if current_user.articles.find(@article)

      @article.readinglist_items.find_by_user_id(current_user.id).touch

      flash[:notice] = "Successfully added article (again)."
      redirect_to @article
  else
    @article.users << current_user
    if @article.save
      flash[:notice] = "Successfully added article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文