【发布时间】:2015-07-30 07:32:53
【问题描述】:
这是我在 User、Gig(product) 和记录购买的 Purchase 表之间的关系。
class User < ActiveRecord::Base
has_many :gigs
has_many :purchases, foreign_key: 'buyer_id'
has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end
class Gig < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases
has_many :sellers, through: :purchases
end
class Purchase < ActiveRecord::Base
belongs_to :gig
belongs_to :buyer, class_name: 'User'
belongs_to :seller, class_name: 'User'
end
记录我在控制器中使用的购买
def downloadpage
ActiveRecord::Base.transaction do
if current_user.points >= @gig.pointsneeded
@purchase = current_user.purchases.create(gig: @gig, seller: @gig.user)
if @purchase
current_user.points -= @gig.pointsneeded
@gig.user.points += @gig.pointsneeded
current_user.save
if @gig.user.save
render 'successful_download', locals:{link:@gig.boxlink}
end
end
else
redirect_to :back, notice: "You don't have enough points"
end
end
end
当买家从卖家那里购买东西时一切正常,积分在账户之间转移,买家被重定向到最终演出。
在我看来我可以做到
<h1>You downloaded <%= current_user.purchases.count %> boxes</h1>
它将显示买家制作的“演出”数量。
现在我想显示的不仅仅是数字,而是他购买的产品的标题和图片。这就是我尝试过的
<div class="row experiment">
<% current_user.purchases.each do |gig| %>
<div class="well for-h1-gig-second col-xs-6 col-sm-4 col-lg-3 ">
<%= link_to (image_tag gig.image.url(:medium), :class=>"img-responsive"), gig %>
<h1><%= link_to gig.title, gig %></h1>
</div>
<% end %>
</div>
但它说,它找不到图像和标题。
所以我尝试了current_user.purchases.gig.each do |gig|
没有成功。
我该如何解决? P.S:请随意编辑我的标题,对于未来的读者,我无法更好地表述它,谢谢。
【问题讨论】:
标签: ruby database ruby-on-rails-4 model-view-controller model