1
class User
include Mongoid::Document
has_many :orders
has_one :payments
end
class Order
include Mongoid::Document
belongs_to :user
has_one :payment, dependent: :destroy, :autosave => true
end
class Payment
include Mongoid::Document
belongs_to :order
belongs_to :user
ebd
user_controller.rb
:如何從祖父訪問孫子文件與mongoid +上<code>jobs</code>行動導軌3
def jobs
#
#
jobs = Order.where(:seller_id => current_user.id).order_by :created_at, :desc
@jobs = []
for j in jobs
if j.payment.preapproval_key != nil && j.payment.correlation_id != nil && j.payment.approved == true
@jobs << j
end
end
#
#
end
可以濾波器payment.preapproval_key != nil && payment.correlation_id != nil && payment.approved == true
所有作業不使用這個循環?
有更好的方法來做到這一點?
所以你想在MongoDB中做一個自連接? –
謝謝!我需要得到這些條件下的所有工作。我不知道是否存在一個最好的辦法做到這一點! – hyperrjas
問題是MongoDB根本不支持連接,所以自連接是不可能的。我認爲你現在採用的方法(這可能會很昂貴)或反規範化和維護重複數據。你可能只是有一個這樣的副本:http://stackoverflow.com/q/8269162/479863 –