显示用户购买时出现错误,显示他购买的产品时出现产品图片和标题错误
Getting error when showing the purchases of the user,when showing the product he bought i get an error for product image and title
这是我在用户、Gig(产品)和记录购买的购买 table 之间的关系。
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>
它将显示 "gigs" 买家制作的数量。
现在我想显示的不仅仅是数字,还有产品的标题和图片他 bought.This 是我试过的
<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|
没有成功。
How do i fix it?
P.S: Please feel free to edit my title,for future readers,i couldn't formulate it better,thank you.
尝试为用户添加关联:
class User < ActiveRecord::Base
has_many :purchases, foreign_key: 'buyer_id'
has_many :gigs, through: :purchases, source: :buyer
has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end
那么你应该可以遍历 current_user.gigs
您的主要问题是 current_user.purchases.each
遍历购买 - 而不是演出。
<div class="row experiment">
<% current_user.purchases.each do |purchase| %>
<% gig = purchase.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>
还有其他一些问题:
class User < ActiveRecord::Base
has_many :gigs # not going to work.
end
它不起作用的原因是 user
和 gig
之间的关系通过 purchases
以及 buyer_id
和 seller_id
外键. Rails 不支持依赖多个键的关系。
如果您想要 select 用户是卖家或买家的演出,您可以使用:
Gig.joins(:purchases).where('purchases.buyer_id=? OR purchases.seller_id=?', [current_user.id, current_user.id])
这是我在用户、Gig(产品)和记录购买的购买 table 之间的关系。
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>
它将显示 "gigs" 买家制作的数量。
现在我想显示的不仅仅是数字,还有产品的标题和图片他 bought.This 是我试过的
<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|
没有成功。
How do i fix it? P.S: Please feel free to edit my title,for future readers,i couldn't formulate it better,thank you.
尝试为用户添加关联:
class User < ActiveRecord::Base
has_many :purchases, foreign_key: 'buyer_id'
has_many :gigs, through: :purchases, source: :buyer
has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'
end
那么你应该可以遍历 current_user.gigs
您的主要问题是 current_user.purchases.each
遍历购买 - 而不是演出。
<div class="row experiment">
<% current_user.purchases.each do |purchase| %>
<% gig = purchase.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>
还有其他一些问题:
class User < ActiveRecord::Base
has_many :gigs # not going to work.
end
它不起作用的原因是 user
和 gig
之间的关系通过 purchases
以及 buyer_id
和 seller_id
外键. Rails 不支持依赖多个键的关系。
如果您想要 select 用户是卖家或买家的演出,您可以使用:
Gig.joins(:purchases).where('purchases.buyer_id=? OR purchases.seller_id=?', [current_user.id, current_user.id])