Collections using has_many: through relationships

I want the current user to read and buy postings in clubs in which he is a member. I would like to do something like the following, but I haven’t clicked anything that I read or didn’t try. I will use many nested 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

Message 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 an error:

undefined `memberships' method for zero: NilClass

To which I am not surprised. In addition, I want to be able to select only those messages that will only be in a certain event, etc.

: . , , . , , .

, , , ActiveRecord. Association Join Models . , .

, , ​​. , , ,

def member?(club) 
   return !!self.clubs.find(club)

, , , - . ? .. , , ...

Logger.info

    @user = current_user
    logger.info @user 
    logger.info 'super secret message'
   # @club_posts = @user.memberships.collect {|a| a.product.posts}

#<User:0x5b202a0>
super secret message

, , "" (!). , , , .. tho

+3
2

, , gem nested_has_many_through, Rails 3.1

  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

<%= render :partial => "order", :collection => @for_my_products, :locals => {:order => @order}  %>

, . , , , , !

0

, current_user - nil. , ?

0

All Articles