【发布时间】:2026-01-21 20:20:02
【问题描述】:
我正在尝试显示属于卖家产品帖子的订单集合
买家是订单中的user_id,卖家是产品中的user_id
快速:
一个用户有很多产品
一个产品有很多帖子(将产品带到多个活动中)
一个帖子有很多订单(其他用户可以订购)
这是我的代码
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me,
:bio, :reason, :barter
#:email, :name, :bio, :reason, :barter, :
has_many :products, :dependent => :destroy
has_many :posts, :dependent => :destroy
has_many :orders, :dependent => :destroy
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :club
belongs_to :category
has_many :posts, :dependent => :destroy
class Post < ActiveRecord::Base
belongs_to :product, :include => [:user]
belongs_to :event
has_many :orders, :dependent => :destroy
accepts_nested_attributes_for :orders
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :post
#schema
create_table "orders", :force => true do |t|
t.float "quantity"
t.float "order_price"
t.integer "post_id"
t.integer "user_id"
t.boolean "payment_received"
t.integer "mark_for_buyer"
t.string "comment_for_buyer"
t.integer "mark_for_seller"
t.string "comment_for_seller"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "orders", ["post_id"], :name => "index_orders_on_post_id"
add_index "orders", ["user_id"], :name => "index_orders_on_user_id"
create_table "posts", :force => true do |t|
t.float "quantity"
t.datetime "deadline"
t.integer "product_id"
t.integer "event_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products", :force => true do |t|
t.string "title"
t.text "desc"
t.text "ingredients"
t.float "deposit"
t.float "cost"
t.string "units"
t.float "quantity"
t.float "deadline_hours"
t.boolean "presell_option"
t.integer "user_id"
t.integer "category_id"
t.integer "club_id"
t.datetime "created_at"
t.datetime "updated_at"
end
订单/index.html.erb
#this one works beautifully
<%= render :partial => "order", :collection => @orders.find_all_by_user_id(current_user.id), :locals => {:order => @order} %>
#this one doesn't work (has_many association resources have been tricky for me)
<%= render :partial => "order", :collection => @orders.where(:post => {:product => {:user_id => current_user.id}}) , :locals => {:order => @order} %>
orders/_order.html.erb
<tr>
<td><%= order.user.name %></td>
<td><%= order.post.product.user.name %></td>
<td><%= link_to order.post.product.title, order.post %></td>
<td><%= number_to_currency order.order_price %></td>
<td><%= order.quantity %></td>
<td><%= order.updated_at.to_formatted_s(:short) %> </td>
<td><%= order.payment_received %></td>
<td><%= link_to 'Show', order %></td>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
现在,一个集合效果很好,订单有一个 user_id 字段,它收集下订单的用户并显示所有正确的订单。 另一个集合不起作用,我收到此错误:
错误
ActiveRecord::StatementInvalid in Orders#index - No attribute named `user_id` exists for table `product`
但“产品”表中有一个“用户 ID”字段。我可以在 cancan 中调用它,以将订单管理限制在拥有所订购产品且工作精美的用户身上
can :update, Order, :post => {:product => {:user_id => user.id }}
非常感谢任何见解!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 collections partial-views has-many-through