我有這方面的關係:BSON :: InvalidDocument:無法序列化類用戶的目標爲BSON
class User
include Mongoid::Document
has_many :orders, :class_name => 'Order', :inverse_of => :user
has_many :sold_orders, :class_name => 'Order', :inverse_of => :seller
end
class Order
include Mongoid::Document
belongs_to :user, :class_name => 'User', :inverse_of => :orders
belongs_to :seller, :class_name => 'User', :inverse_of => :sold_orders
end
如果我要得到我所運行的所有訂單,目前賣家在我的控制器是這樣的:
def jobs
@orders = Order.where(seller: current_user)
p @orders
respond_to do |format|
format.html { render :layout => 'centered'}
end
end
我可以在我的日誌控制檯中看到:
#<Mongoid::Criteria
selector: {:seller=>#<User _id: 500541f81d41c82f1b000037, _type: "User", created_at: 2012-07-17 10:44:08 UTC, ....more attributes>},
options: {},
class: Order,
embedded: false>
這是錯誤的,因爲我得到了用戶,但我想獲得的所有訂單立方米rrent賣家。
如何獲得當前銷售商的所有訂單?
謝謝!
http://stackoverflow.com/questions/5523582/mongoid-scope-with-referenced-object-as-criteria/5556041#5556041 – rubish 2012-08-17 16:07:59
我使用的是mongoid 2.4.12。它對我不起作用:(用@orders = Order.where(:seller_id => current_user.id)'非常感謝! – hyperrjas 2012-08-17 16:17:42
那麼,這個例外在哪裏引發? – 2012-08-17 17:08:51