Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to collect through multiple sub categories
    primarykey
    data
    text
    <p>What's the rails way to see posts nested beneath several associative models that a user habtm? </p> <pre><code> @user = current_user @user_clubs = @user.clubs #user is a member of many clubs (habtm), clubs have many events to post a quantity of products to @uc_products = @user_clubs.collect {|a| a.products} # clubs have many products (and categories, haven't implemented yet) (with title, description, etc) # @ucp_posts = @uc_categories.collect {|a| a.posts} # products have many posts (product_id, quantity, &amp; date offered only) </code></pre> <p>the logger gives me collections, so I know the code is working up till that point</p> <pre><code>#&lt;User:0x58d4300&gt; #&lt;Club:0x5aa82e8&gt;#&lt;Club:0x5aa3578&gt; #&lt;Product:0x59150e8&gt;#&lt;Product:0x5911bc0&gt;#&lt;Product:0x58582b0&gt; </code></pre> <p>I can collect products, but as soon as I try and collect posts from that, it gives me the error</p> <pre><code>undefined method `posts' for #&lt;Class:0x5a248d0&gt; </code></pre> <p>I've tried :include, both directions, to no avail.<br> Edit: here's most of my models: (I thought it might crowd things before, didn't include)</p> <pre><code> class Post &lt; ActiveRecord::Base belongs_to :product, :include =&gt; :club belongs_to :event class Product &lt; ActiveRecord::Base belongs_to :user belongs_to :club belongs_to :category has_many :posts class Club &lt; ActiveRecord::Base has_many :products, :include =&gt; :posts has_many :events belongs_to :users_clubs has_many :users_clubs has_many :users, :through =&gt; :users_clubs, :foreign_key =&gt; :users_club_id class UsersClub &lt; ActiveRecord::Base #table for joining habtm has_many :users has_many :clubs belongs_to :user belongs_to :club class Event &lt; ActiveRecord::Base has_many :posts belongs_to :club 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, : belongs_to :roles_users has_many :roles_users has_many :roles, :through =&gt; :roles_users belongs_to :users_clubs has_many :users_clubs has_many :clubs, :through =&gt; :users_clubs, :foreign_key =&gt; :users_club_id has_many :approvals, :dependent =&gt; :destroy has_many :products, :dependent =&gt; :destroy has_many :posts, :dependent =&gt; :destroy has_many :orders, :dependent =&gt; :destroy </code></pre> <p>My research:<br /> I've discovered tree structures, but the structure stays within it's own model, so that wasn't fruitful. I've looked through <a href="http://guides.rubyonrails.org/association_basics.html" rel="nofollow noreferrer">http://guides.rubyonrails.org/association_basics.html</a> as well, and could only get as far as I did. I tried looping through the products once, but that gave me the error 'do' and 'end' were both unexpected. I thought of searching for 'multiple tags query' but the results weren't going 4 levels deep, so that didn't help much either. I'd include extra columns in my table to make it easy, but I wanted to keep things DRY.</p> <p>Whatdya think? Or what would be a good search term for me to try on google? Any help is much appreciated for this noob.</p> <p>Edit 2<br/> Found something here, will test later<br /> <a href="https://stackoverflow.com/questions/5134723/rails-associations-through-multiple-levels">Rails Associations Through Multiple Levels</a> (no luck)<br /> <a href="https://stackoverflow.com/questions/5943064/rails3-how-to-multi-level-associations">How to multi-level Associations?</a> (looks promising)<br /></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.
 

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