Collections using has_many, :through relationships
I want the current user to read and purchase postings in clubs in which he is a member. I would love to do something like the following, but nothing I've read or tried has clicked yet. I will be using many nesting collections of this type, I'm stuck!
Models class User < ActiveRecord::Base
has_many :memberships
has_many :clubs, :through => :memberships
has_many :products
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :club
class Club < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_many :events
class Event < ActiveRecord::Base
belongs_to :club
class Product < ActiveRecord::Base
belongs_to :user
has_many :posts
class Post < ActiveRecord::Base
belongs_to :product
belongs_to :event
Posts Controller
def index
@user = current_user
@posts_in_my_clubs = @user.memberships.collect { |a| a.products.posts}
posts/index.html.erb
@posts_in_my_clubs do |post|
Gives me the error:
undefined method `memberships' for nil:NilClass
To which I am not surprised. Additionally I want to be able to select only the posts that will be only at a certain event, etc.
A summary of the models: Clubs have many Events & Users. Users have many Products to which they can Post a quantity to sell at different Event dates. Only Users who are members of a Club can post or view posts in a Club.
Instead of repeating data in the models, I have made associations, which seem to make the data inaccessible to creating collections in ActiveRecord. I've found in my research Association Join Models, and Intelligent association mapping with metawhere. These links make me feel like I'm getting close.
Edit
I'm signed in and can access all of the data I need as long as I'm not trying to do a collection like this. For instance I can check if I'm a member of the club by
def member?(club)
return !!self.clubs.find(club)
I am a noob, so I must be missing something obvious here. Do I need to break up my collection into steps? i.e. collect开发者_如何学Go user clubs, collect user clubs products, collect user clubs products posts... thanks
Logger.info
@user = current_user
logger.info @user
logger.info 'super secret message'
# @club_posts = @user.memberships.collect {|a| a.product.posts}
returns
#<User:0x5b202a0>
super secret message
When I remove the comment, I get the nil error again, for 'product' now (progress!). I sure thought I was logged in before.. tho it was late
That error message means that current_user
is nil
. Are you sure your authentication system is working?
Here is the solution I found, using gem nested_has_many_through, which is standard in Rails 3.1
User model
has_many :products, :dependent => :destroy
has_many :orders, :dependent => :destroy
has_many :product_posts, :through => :products, :source => :posts, :uniq => true
has_many :food_orders, :through => :product_posts, :source => :orders, :uniq => true
OrdersController
@orders_for_my_products = current_user.food_orders.all
Orders index
<%= render :partial => "order", :collection => @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!
精彩评论