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”为“Noun”的所有约束。

希望有任何关于到达那里的建议。

4

1 回答 1

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

请参阅指南中的条件加入

于 2012-05-12T01:37:12.770 回答