2012-05-12 47 views
1

我有以下型号:如何对存储在其他模型中的属性模型执行查询?

class Constraint < ActiveRecord::Base 
    belongs_to :constraint_category 
end 


class ConstraintCategory < ActiveRecord::Base 
    has_many :constraints 
end 

该机型具有这些属性(从DB/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 

我想创建一个查找所有约束的查询,其中“活动“属性为”真“,”约束_类别。值“为”名词“。

想得到那里的任何建议。

回答

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

请参阅指南中的conditionsjoins

+0

谢谢!这工作。 –

+0

更简洁一点Constraint.joins(:constraint_category).where(:active => true,:constraint_category => {:value =>'Noun'}) – bcd

相关问题