Reputation: 156
I am trying to show a collection of orders that belongs to a sellers products posts
the buyer is the user_id in the order, the seller is the user_id in the product
Quickly:
A user has many products
A product has many postings (to bring products to multiple events)
A post has many orders (other users can order)
Here's my code
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
#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} %>
<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>
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:
ActiveRecord::StatementInvalid in Orders#index - No attribute named `user_id` exists for table `product`
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
can :update, Order, :post => {:product => {:user_id => user.id }}
Any insight is much appreciated!
Upvotes: 0
Views: 2929
Reputation: 156
I've found the trick is this gem http://rubygems.org/gems/nested_has_many_through which can do something like this:
class Author < User
has_many :posts
has_many :categories, :through => :posts, :uniq => true
has_many :similar_posts, :through => :categories, :source => :posts
has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
has_many :commenters, :through => :posts, :uniq => true
end
class Post < ActiveRecord::Base
belongs_to :author
belongs_to :category
has_many :comments
has_many :commenters, :through => :comments, :source => :user, :uniq => true
end
This has super-simplified my queries and collections.
This is all the code I ended up needing. I'm told the gem nested_has_many_through will be standard in Rails 3.1
has_many :products, :dependent => :destroy
has_many :product_posts, :through => :products, :source => :posts, :uniq => true
has_many :food_orders, :through => :product_posts, :source => :orders, :uniq => true
@orders_for_my_products = current_user.food_orders.all
<%= render :partial => "order", :collection => @orders_for_my_products, :locals => {:order => @order} %>
This returns the deep nested data via relationships using minimal code. I sure hope its efficient, but at least my sanity is spared!
Upvotes: 0
Reputation: 80041
Check out the Law of Demeter. It explains why what you're doing is causing problems (no separation of concerns = spaghetti code). Also check out ActiveSupport::Delegate to see how you can concisely apply the Law of Demeter to your Rails app and avoid lists of boring getters and setters.
A few notes to help you out:
where
in your controller. Your view should have arrays (or ActiveSupport::Proxy's) to simply iterate over and display. You should avoid having your view trigger database queries, and you should never keep record-fetching business logic in them.@comment.post.title
, define and use @comment.post_title
instead (note the underscore). This avoids requiring your view to have innate knowledge of your database schema and model associations. It simply needs to know that a comment has a post title, however that data is modeled.If you refactor your code to keep each model focused on a single concern and modeling a single resource, then layer the associations on top of that in a clean, compartmentalized fashion, you will surely obviate the need for someone to solve this single question for you (teach a man to fish…).
Upvotes: 3