2012-02-11 106 views
0

您好,感謝您提前給予幫助。RoR:在購買的產品和用戶之間建立關係

我想在保存訂單時創建用戶和產品(pdf)之間的關係。

class PDF 
    has_many :line_items 
end 

class LineItem 
    belongs_to :pdf 
    belongs_to :cart 
end 

class Cart 
    has_many :line_items 
    has_one :order 
end 

class Order 
    belongs_to :cart 
end 

在用戶購買line_item時,我想通過用戶和pdf之間的連接模型(pdf_relationships)創建關係。

我正試圖在給定的購物車中找到每個PDF(由foreign_key line_item.pdf_id找到),並在用戶和購物車中的每個pdf之間創建pdf_relationships。我將使用戶的ID爲所有者ID,並使pdf的ID爲owned_id。

我的訂單控制器看起來是這樣的:

def create 
    @order = current_cart.build_order(params[:order]) 
    @order.ip_address = request.remote_ip 
    if @order.save 
    if @order.purchase 
     render :action => "success" 
    else 
     render :action => "failure" 
    end 
    else 
    render :action => 'new' 
    end 
end 

什麼我遇到的麻煩是:

class Order 
    belongs_to :cart 

    before_save :create_owner 

    ***def create_owner 
    self.cart.line_items.each do |item| 
     pdf.find_by_item_pdf_id(:pdf_id) 
     current_user.pdf_relationships.build(:owned_id => pdf.id) 
    end 
    end*** 
end 

這裏是我的用戶模型:

class User 
    has_many :line_items 

    has_many :pdf_relationships, foreign_key: :owner_id, :dependent => :destroy 
    has_many :pdfs, foreign_key: :user_id, dependent: :destroy 
    has_many :pdf_ownings, :through => :pdf_relationships, :source => :owned 

def owning?(owned) 
    pdf_relationships.find_by_pdf_owned_id(owned) 
    end 

    def own!(owned) 
    pdf_relationships.create!(:owned_id => owned.id) 
    end 

    def unown!(owned) 
    pdf_relationships.find_by_pdf_owned_id(owned).destroy 
    end 

我希望這已經夠清楚了。我一直試圖弄清楚這個問題,現在肯定是想成爲一名新手。暗示性的讀物也絕對受歡迎!

回答

0

它看起來像你有正確的想法。

由於LineItem有很多PDF S,你可以使用來自LineItempdf_id沒有費心去從數據庫讀取記錄。您也可以將這些ID添加到現有的一組用戶PDF關聯中,就好像您只是將項目推送到數組中一樣。

但是,您的模型將無法訪問當前會話(因爲這是由控制器處理的),因此您必須以其他方式將當前用戶傳遞給Order,可能作爲purchase方法的一部分。

class Order 
    belongs_to :cart 

    def purchase(user) 
    # ... existing logic ... 

    user.pdf_relationship_ids << cart.line_items.map(&:pdf_id) 
    end 
end 

您還必須聲明UserPDF之間的關聯,並創建了遷移,但它聽起來像你已經打算這樣做。


更新:我想你可以通過服用has_many :through優勢極大地簡化您的User模型。這是我設想的:

class User < ActiveRecord::Base 
    has_many :orders 
    has_many :line_items, :through => :orders 
    has_many :pdfs, :through => :line_items 
end 

class Order < ActiveRecord::Base 
    belongs_to :user 
    has_many :line_items 
end 

class LineItem < ActiveRecord::Base 
    belongs_to :order 
    has_one :pdf 
end 

class Pdf < ActiveRecord::Base 
    belongs_to :line_item 
end 

然後,您不必明確指定用戶擁有PDF。它隱含在用戶 - >訂單 - >訂單項 - > PDF關係中。

這可能無法解決您的問題。如果不刪除原始訂單項,您將無法「拒絕」PDF,這可能不是您想要的。但我認爲你應該努力瞄準這樣的事情。儘可能地利用Rails的內置關聯。

+0

我想我一定是在正確的方向前進。我想,當我嘗試通過遍歷購物車中的每個line_item(cart.line_item.pdf_id.each ...)來查找pdf並查看每個line_item.pdf_id時,問題就出現了。一旦我得到這些ID,那麼我可以創建PDF關係。總的來說,我需要採取每個pdf.id這是line_item.pdf_id並將其作爲owned.id。 – Jches 2012-02-11 03:55:08

+0

你還在收到錯誤?你能使用我的示例代碼嗎?如果不是,你的'User'模型是什麼樣的? – Brandan 2012-02-11 04:13:19

+0

是的,我仍然有錯誤。我編輯了我的問題並添加了用戶模型。我的代碼def create_owner在訂單模型中甚至遠程更正? – Jches 2012-02-11 20:56:12