2012-05-12 12 views
1

私は次のモデルがあります:他のモデルに格納された属性を持つモデルでクエリを実行する方法は?

class Constraint < ActiveRecord::Base 
    belongs_to :constraint_category 
end 


class ConstraintCategory < ActiveRecord::Base 
    has_many :constraints 
end 

モデルは、これらの属性(デシベル/ schema.rbから)持っている:

create_table "constraint_categories", :force => true do |t| 
    t.string "value" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.boolean "active" 
    end 

    create_table "constraints", :force => true do |t| 
    t.string "phrase" 
    t.integer "constraint_category_id", :limit => 255 
    t.boolean "active" 
    t.datetime "created_at",       :null => false 
    t.datetime "updated_at",       :null => false 
    end 

を私は「すべての制約を発見するクエリを作成したいです「active」属性は「true」であり、「constraint_category.value」は「Noun」である。

そこに行く際のアドバイスが大好きです。

答えて

0
Constraint.joins(:constraint_category).where('constraints.active = ? and constraint_categories.value = ?', true, 'Noun') 

ガイドのconditionsおよびjoinsを参照してください。

+0

ありがとうございます!それはうまくいった。 –

+0

もう少しクリーナーです。Constraint.joins(:constraint_category).where(:active => true、:constraint_category => {:value => '名詞}}) – bcd

関連する問題