2012-01-28 17 views
0

"カテゴリ"のモデルを書いた。ここでの要件は、各カテゴリが1つのカテゴリ「タイプ」に分類できることです。私はこのプロジェクトを行うのと同時にレールを学習しており、上記のクラスメソッド(where_category_type)で上記の作業を行うことができました。これをもっときれいにするような文法的な砂糖はありますか?

class Category < ActiveRecord::Base 

    #associations 
    belongs_to :category_type 
    has_and_belongs_to_many :recipes 

    def self.where_category_type category_type 
    Category.find(:all, :include => :category_type, :conditions => { :category_types => {:name => category_type }}) 
    end 

end 

すべての作品などが、私は「レールウェイ」の事をやっていることを確認するために非常に熱心だので、私は、これはもう少しなるだろういくつかのシンタックスシュガーのどこかに欠けている場合、私は思っていました読める/あまり冗長ではない?代わりに* where_category_type *カテゴリーのクラスの静的メソッドを定義する

答えて

1
class Category < ActiveRecord::Base 

    #associations 
    belongs_to :category_type 
    has_and_belongs_to_many :recipes 

end 

次に、あなただけ呼び出すことができます。

Category.joins(:category_type).where('category_types.name' => 'name of your category').all 
関連する問題