Note that there are some explanatory texts on larger screens.

plurals
  1. PORender a partial with a collection through multiple levels of has_many associations
    primarykey
    data
    text
    <p>I am trying to show a collection of orders that belongs to a sellers products posts</p> <p>the buyer is the user_id in the order, the seller is the user_id in the product</p> <p>Quickly:<br /> A user has many products<br /> A product has many postings (to bring products to multiple events)<br /> A post has many orders (other users can order)<br /></p> <p>Here's my code</p> <pre><code> class User &lt; 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 =&gt; :destroy has_many :posts, :dependent =&gt; :destroy has_many :orders, :dependent =&gt; :destroy class Product &lt; ActiveRecord::Base belongs_to :user belongs_to :club belongs_to :category has_many :posts, :dependent =&gt; :destroy class Post &lt; ActiveRecord::Base belongs_to :product, :include =&gt; [:user] belongs_to :event has_many :orders, :dependent =&gt; :destroy accepts_nested_attributes_for :orders class Order &lt; ActiveRecord::Base belongs_to :user belongs_to :post #schema create_table "orders", :force =&gt; 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 =&gt; "index_orders_on_post_id" add_index "orders", ["user_id"], :name =&gt; "index_orders_on_user_id" create_table "posts", :force =&gt; 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 =&gt; 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 </code></pre> <h1>orders/index.html.erb</h1> <pre><code> #this one works beautifully &lt;%= render :partial =&gt; "order", :collection =&gt; @orders.find_all_by_user_id(current_user.id), :locals =&gt; {:order =&gt; @order} %&gt; #this one doesn't work (has_many association resources have been tricky for me) &lt;%= render :partial =&gt; "order", :collection =&gt; @orders.where(:post =&gt; {:product =&gt; {:user_id =&gt; current_user.id}}) , :locals =&gt; {:order =&gt; @order} %&gt; </code></pre> <h1>orders/_order.html.erb</h1> <pre><code> &lt;tr&gt; &lt;td&gt;&lt;%= order.user.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.post.product.user.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to order.post.product.title, order.post %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= number_to_currency order.order_price %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.quantity %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.updated_at.to_formatted_s(:short) %&gt; &lt;/td&gt; &lt;td&gt;&lt;%= order.payment_received %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Show', order %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_order_path(order) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', order, :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Now, one collection works great, where the order has a user_id field, it collects the user that made the order and shows all the correct orders. The other collection doesn't work, and I'm getting this error:</p> <h1>Error</h1> <pre><code>ActiveRecord::StatementInvalid in Orders#index - No attribute named `user_id` exists for table `product` </code></pre> <p>But there IS a 'user_id' field in the 'product' table. I am able to call this in cancan to limit management of orders to the user who owns the product that is ordered, and works beautifully</p> <pre><code>can :update, Order, :post =&gt; {:product =&gt; {:user_id =&gt; user.id }} </code></pre> <p>Any insight is much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload