简体   繁体   English

通过以下方式在has_many上优雅地设置联接表属性:

[英]Elegantly set join table attributes on has_many through: association

In my Rails 3.2 app Users can create Collections of Articles . 在我的Rails 3.2应用程序中, Users可以创建Articles Collections Collections and Articles are joined by a has_many through: collections_articles relationship. CollectionsArticles是由加入has_many through: collections_articles关系。

I wish to store the user_id in the collections_articles join table when an Article is added to a Collection . 当将Article添加到Collection时,我希望将user_id存储在collections_articles连接表中。 (So the app can later show who added an article to a collection). (因此,该应用程序可以稍后显示谁将文章添加到集合中)。 Is there an elegant way to do this? 有没有一种优雅的方法可以做到这一点?

class CollectionsArticle
  collection_id
  article_id
  user_id

  belongs_to :collection
  belongs_to :article
  belongs_to :user

When a user adds an article to a collection I'd just like to use the shovel, so: 当用户将文章添加到集合中时,我只想使用铲子,所以:

  my_collection.articles << my_article

Is there anyway to (elegantly) simultaneously set user_id of the join table to current_user ? 无论如何,要(优雅地)同时将user_id表的user_id设置为current_user吗? Or is the best way just to explicitly create the record in the join table itself: 或者最好是在联接表本身中显式创建记录的最佳方法:

  CollectionsArticle.create(article: @article, collection: @collection, user: current_user)

Thanks! 谢谢!

The model layer itself does not have knwoledge of the current_user , so you have to pass it in from the controller layer somehow. 模型层本身不具有current_user ,因此您必须以某种方式从控制器层传递它。

The most elegant solution I can come up with is to create an add_article -method in the Collection model: 我能想到的最优雅的解决方案是在Collection模型中创建一个add_article方法:

def add_article(article, by_user)
  self.collection_articles.create(article: article, user: by_user)
end

..and call that from the controller ..然后从控制器中调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建has_many:通过关联并设置联接模型属性 - How to create has_many :through association and set join model attributes 如何通过关联在has_many:的联接表中编辑和更新属性? - How to edit and update attributes in join table in has_many: through association? 通过关联为has_many中的联接表属性的嵌套表单输入,不允许使用参数 - Nested form input for join table attributes in a has_many through association, parameter is unpermitted 如何从关联访问联接表属性(has_many:through) - How to access join table attributes (has_many :through) from association 在视图中显示联接表行(has_many通过关联) - Displaying join table row in view (has_many through association) 通过Rails 4中的关联,联接表和has_many出现问题 - Issue with join table and has_many through association in Rails 4 通过关联将额外字段添加到has_many中的联接表中 - Adding an extra field to the join table in has_many through association 我是否需要has_many的联接表:通过关联? - Do I need a join table for a has_many :through association? Rails has_many:通过关联:将实例保存到联接表中 - Rails has_many :through association: save instance into join table 优雅地从has_many中选择属性:通过Rails中的连接模型 - Elegantly selecting attributes from has_many :through join models in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM