开发者

how to collect through multiple sub categories

What's the rails way to see posts nested beneath several associative models that a user habtm?

    @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, & date offered only)

the logger gives me collections, so I know the code is working up till that point

#<User:0x58d4300>
#<Club:0x5aa82e8>#<Club:0x5aa3578>
#<Product:0x59150e8>#<Product:0x5911bc0>#<Product:0x58582b0>

I can collect products, but as soon as I try and collect posts from that, it gives me the error

undefined method `posts' for #<Class:0x5a248d0>

I've tried :include, both directions, to no avail.

Edit: here's most of my models: (I thought it might crowd things before, didn't include)

        class Post < ActiveRecord::Base
          belongs_to :product, :include => :club
     开发者_JAVA百科     belongs_to :event

    class Product < ActiveRecord::Base
        belongs_to :user
        belongs_to :club
        belongs_to :category
        has_many :posts

    class Club < ActiveRecord::Base
        has_many :products, :include => :posts
            has_many :events
            belongs_to :users_clubs
            has_many :users_clubs
            has_many :users, :through => :users_clubs, :foreign_key => :users_club_id

class UsersClub < ActiveRecord::Base  #table for joining habtm
    has_many :users
    has_many :clubs
    belongs_to :user
    belongs_to :club

class Event < ActiveRecord::Base
    has_many :posts
    belongs_to :club


    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, :

      belongs_to :roles_users                
      has_many :roles_users
      has_many :roles, :through => :roles_users

      belongs_to :users_clubs                
      has_many :users_clubs
      has_many :clubs, :through => :users_clubs, :foreign_key => :users_club_id

      has_many :approvals, :dependent => :destroy
      has_many :products, :dependent => :destroy
      has_many :posts, :dependent => :destroy
      has_many :orders, :dependent => :destroy

My research:

I've discovered tree structures, but the structure stays within it's own model, so that wasn't fruitful. I've looked through http://guides.rubyonrails.org/association_basics.html 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.

Whatdya think? Or what would be a good search term for me to try on google? Any help is much appreciated for this noob.

Edit 2

Found something here, will test later

Rails Associations Through Multiple Levels (no luck)

How to multi-level Associations? (looks promising)


Looks like you're missing a has_many association on Product, can you post that code?

It should resemble:

class Product < ActiveRecord::Base
  has_many :posts
end


After weeks of toil on this..

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜