如何通过多个子类别收集 [英] how to collect through multiple sub categories
问题描述
查看嵌套在用户 habtm 的几个关联模型下的帖子的方法是什么?
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.
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
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
我的研究:
我发现了树状结构,但该结构仍保留在它自己的模型中,因此没有成果.我也浏览过 http://guides.rubyonrails.org/association_basics.html,并且只能达到我的目的.我尝试循环浏览产品一次,但这给了我错误do"和end"都是意外的.我想搜索多标签查询",但结果没有深入 4 级,因此也没有太大帮助.为了方便起见,我会在表格中添加额外的列,但我想让事情保持干燥.
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.
编辑 2
在这里找到了一些东西,稍后测试
Rails 多层次关联(运气不好)
如何进行多级关联?(看起来很有希望)
Edit 2
Found something here, will test later
Rails Associations Through Multiple Levels (no luck)
How to multi-level Associations? (looks promising)
推荐答案
经过数周的努力..
我发现诀窍是这个 gem http://rubygems.org/gems/nested_has_many_through可以做这样的事情:
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.
这篇关于如何通过多个子类别收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!