我有一个关于多对多关系的问题,特别是has_many:通过关联。如何使用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李
谢谢您的回答,因为我有过相关联的的has_many我的文章模型是这样的:
has_many :readinglist_items, :dependent => :destroy
has_many :users, :through => :readinglist_items
,所以我不知道我是否可以将一个:touch => true
添加到has_many,因为我只想在连接表中进行特定的输入。
创建操作中更新的要点是,如果用户添加了过去已添加的文章,则会将文章移动到顶部(而不是再次添加)。
请注意,只有将连接表设置为常规create_table类型并因此具有作为主键的id列时,readinglist_items记录上的.touch才会起作用。如果您使用create_join_table设置联接,那么您将没有主键,因此您无法触摸该记录。答案是通过进一步的迁移添加主键: add_column:readinglist_items,:id,:primary_key,first:true – IrishDubGuy 2017-10-03 23:49:07