导轨的has_many:通过和设置属性上加入模型 [英] Rails has_many :through and Setting Property on Join model

查看:123
本文介绍了导轨的has_many:通过和设置属性上加入模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似<一个href=\"http://stackoverflow.com/questions/408872/rails-has-many-through-find-by-extra-attributes-in-join-model\">this问题,我怎么设置的连接模型的属性之前在这种情况下救?

Similar to this question, how do I set a property on the join model just before save in this context?

class Post < ActiveRecord::Base
  has_many :post_assets
  has_many :assets, :through => :post_assets
  has_many :featured_images, :through => :post_assets, :class_name => "Asset", :source => :asset, :conditions => ['post_assets.context = ?', "featured"]

end

class PostAssets < ActiveRecord::Base
  belongs_to :post
  belongs_to :asset

  # context is so we know the scope or role
  # the join plays
  validates_presences_of :context
end

class Asset < ActiveRecord::Base
  has_many :post_assets
  has_many :posts, :through => :post_assets
end

我只是希望能够做到这一点:

I just want to be able to do this:

@post = Post.create!(:title => "A Post")
@post.featured_images << Asset.create!(:title => "An Asset")
# ...
@post = Post.first
@featured = @post.featured_images.first
  #=> #<Asset id: 1, title: "An Asset">
@featured.current_post_asset #=> #<PostAsset id: 1, context: "featured">

如何将这项工作?我整天都撞我的头了它。)

How would that work? I've been banging my head over it all day :).

目前是什么情况是,当我这样做:

What currently happens is when I do this:

@post.featured_images << Asset.create!(:title => "An Asset")

然后加入模型 PostAsset 时生成从来没有得到一个机会来设置背景。如何设置这方面的财产?它看起来是这样的:

Then the join model PostAsset that gets created never gets a chance to set context. How do I set that context property? It looks like this:

PostAsset.first #=> #<PostAsset id: 1, context: nil>

更新

我已经创建了一个测试宝石,试图找出问题。是否有更简单的方法来做到这一点?!

I have created a test gem to try to isolate the problem. Is there an easier way to do this?!

这<一个href=\"http://github.com/viatropos/acts-as-joinable/blob/master/lib/acts_as_joinable/core.rb\">ActsAsJoinable::Core类使得它,所以你可以有很多用在连接模式之间的上下文关系很多。并把它添加辅助方法。基本的<一个href=\"http://github.com/viatropos/acts-as-joinable/blob/master/test/test_acts_as_joinable.rb\">tests显示基本上我想要做的。如何做到这一点的任何更好的想法是否正确?

This ActsAsJoinable::Core class makes it so you can have many to many relationships with a context between them in the join model. And it adds helper methods. The basic tests show basically what I'm trying to do. Any better ideas on how to do this properly?

推荐答案

看一下ActiveRecord的中的has_many选项::协会:: ClassMethods API位于:的http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001316

Look at the has_many options in the ActiveRecord::Associations::ClassMethods API located here: http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001316

这是最有趣的报价:

:条件

指定关联对象必须满足的条件是包括一个WHERE如果使用哈希SQL片段,如联想授权= 1.记录创作的作用域。的has_many:帖子,:条件=> {:已发布=>真}将创建刊登了@ blog.posts.create或@ blog.posts.build帖子。

Specify the conditions that the associated object must meet in order to be included as a WHERE SQL fragment, such as authorized = 1. Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.

所以我相信你的条件,必须指定为哈希,像这样:

So I believe your conditions must be specified as a hash, like so:

class Post < ActiveRecord::Base
  has_many :post_assets
  has_many :featured_post_assets, :conditions => { :context => 'featured' }

  has_many :assets, :through => :post_assets

  has_many :featured_images, :through => :featured_post_assets,
           :class_name => "Asset", :source => :asset,
end

和你也应该做到以下几点:

And you should also do the following:

@post.featured_images.build(:title => "An asset")

而不是:

@post.featured_images << Asset.create!(:title => "An Asset")

这应该叫资产范围内的建设,如在报价上面建议的背景下字段添加到资产。它也将节省在同一时间在一个原子事务中加入模型对象(post_asset)和资产对象到数据库中。

This should call the scoped asset build, as suggested in the quote above to add the context field to asset. It will also save both the join model object (post_asset) and the asset object to the database at the same time in one atomic transaction.

这篇关于导轨的has_many:通过和设置属性上加入模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆