JamesRocky
JamesRocky

Reputation: 721

It shows nil in the database, can this solve the main issue?

If a seller sells something, I'm unable to show the user who bought it. With a Sale, Product, and User models

<% @sales.each do |sale| %>
    <tr>
    <td><%= link_to sale.product.title, pickup_path(sale.guid) %></td>
    <td><%= time_ago_in_words(sale.created_at) %> ago</td>
    <td><%= sale.seller_email %></td>
    <td><%= sale.amount %></td>

If I go ahead and change it to <%= sale.buyer_email %>, it just shows me the current user and what THEY just bought rather than WHO bought their item. This is what I get after checking the console, the seller_email is nil and the amount is nil for the last sale. How do I fix this so that the seller can see who go their item?

Upvotes: 0

Views: 66

Answers (2)

sameera207
sameera207

Reputation: 16629

it seems like your current_user in the Transaction controller is nil.

I can see you are missing the before_action :authenticate_user! in the Transaction Controller.

So you could check this by using a debug gem like pry , try to add binding.pry just before product.sales.create!(buyer_email: current_user.email) and see if the current_user has a value

HTH

Upvotes: 0

Kamesh
Kamesh

Reputation: 1465

Actually, the model structure should be changed to create a correct architecture.

You have User, Product and Sale models. So the Association should be like the following.

class User
  has_many :products
  has_many :sales
  has_many :customers, :through => :sales
end

class Product
  has_many :sales
  belongs_to :user
  has_many :buyers, :through => :sales
  has_many :sellers, :through => :sales
end

class Sale
  belongs_to :seller, :class_name => "User"
  belongs_to :buyer, :class_name => "User"
  belongs_to :product
end

Then you can access all the buyers & seller of the product by following line of codes.

product.buyers
product.sellers

Upvotes: 1

Related Questions