如何使用 has_many :through 关联更新连接模型?
我有一个关于多对多关系的问题,特别是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,这不是控制器问题,而是模型问题。您可以指定
:touch =>
。这将使每当子项更新时,关联的belongs_to
关系上为 trueupdate_at
都会得到很好的更新。因此,在您的
Article
模型中放入这样的内容:另外,切线相关:从代码中不清楚您的代码到底在做什么,但看起来也许您正在放置
create
和update
功能都在create
方法中,而不是适当地将它们分开。If I'm understanding you correctly, this isn't a controller issue, but a model issue. You can specify
:touch => true
on abelongs_to
relationship. This will make it so that whenever the child is updated, the association'supdate_at
is updated well.So put something like this in your
Article
model: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
andupdate
functionality both in in thecreate
method instead of splitting them up appropriately.我解决了! (readinglist_item是连接表的名称):
I solved it! (readinglist_item is the name of the join table):