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

"active" 属性が "true" で "constraint_category.value" が "名詞" であるすべての制約を検索するクエリを作成したいと思います。

そこに着くためのアドバイスがあれば幸いです。

4

1 に答える 1

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

ガイドの条件結合を参照してください。

于 2012-05-12T01:37:12.770 に答える