我遇到了使用has_and_belongs_to_many关系的问题。我有以下几点:Mongoid关系查询语法
class User
include Mongoid::Document
has_and_belongs_to_many :subjects
end
class Subject
include Mongoid::Document
field :name, :type => String
attr_accessible :name
has_and_belongs_to_many :users
end
然后在我的控制,我试图找到具有特定主题的所有用户:
class UsersController < ApplicationController
def index
@users = User.where('subjects.name' => 'Physics')
end
end
我要对这个正确的方式?
是你的主题名是primary_key? – 2013-03-07 10:19:35