Getting error when showing the purchases of the us

2019-08-09 19:45发布

问题:

This is the relationship i have between User,Gig(product),and Purchase table that records the 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

To record the purchase i use in my controller

 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

everything works when the buyer buys something from the seller,the points are transferred between accounts,and the buyer gets redirected to the final gig.

In my views i can do

<h1>You downloaded <%= current_user.purchases.count %> boxes</h1>

It will show the number of "gigs" the buyer made.

Now i want to show not just the number,but the title and the picture of the product he bought.This is what i tried

<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>

But it says,that it can not find the image and the title. so i tried current_user.purchases.gig.each do |gig| without success.

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.

回答1:

Your main problem is that current_user.purchases.each iterates through purchases - not gigs.

<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>

Also to some of the other issues:

class User < ActiveRecord::Base
  has_many :gigs # not going to work.
end

The reason it's not going to work is that the relation between user and gig goes through purchases and the buyer_id and seller_id foreign keys. Rails does not support relations that depend on multiple keys.

If you want to select gigs where the user is either seller or buyer you could use:

Gig.joins(:purchases).where('purchases.buyer_id=? OR purchases.seller_id=?', [current_user.id, current_user.id])


回答2:

Try adding the association on user:

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

Then you should be able to loop through current_user.gigs