我遇到了使用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