2011-07-08 6 views
0

私はRuby on Rails 3.0.7を使用していますが、これを実行した後でActiveRecord::Relationクラスを保持したいと思いますarticle_category_relationshipsは)has_many :through録音協会を表しています。反復操作後に `has_many:through`レコードアソシエーションをActiveRecord :: Relationクラスとして維持します。

# @article.article_category_relationships.class    # => ActiveRecord::Relation 
@article.article_category_relationships.map(&:category).class # => Array 

それはこのようなものでなければなりません:map方法はActiveRecord::Relation CLを失うことになると思われ

@article.article_category_relationships.map(&:category).class # => ActiveRecord::Relation 

お尻。

反復メソッドの実行後にActiveRecord::Relationクラスを保持することはできますか?もしそうなら、どのようにすることができますか?

:私はActiveRecord::Relationクラス(.where(...).count、...)のためのRuby on Railsに提供するすべてのメソッドを使用して保つためにそれをしたいと思います。

答えて

0

あなたはルビー1.9を使用している場合は、チェックアウトタップ:

obj.tap{|x|...} => obj 
Yields x to the block, and then returns x. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain. 

    (1..10)    .tap {|x| puts "original: #{x.inspect}"} 
     .to_a    .tap {|x| puts "array: #{x.inspect}"} 
     .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"} 
     .map { |x| x*x }  .tap {|x| puts "squares: #{x.inspect}"} 
関連する問題