如何查看嵌套在用户拥有的几个关联模型下的帖子?
@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)
记录器给我集合,所以我知道代码一直在工作,直到
#<User:0x58d4300>
#<Club:0x5aa82e8>#<Club:0x5aa3578>
#<Product:0x59150e8>#<Product:0x5911bc0>#<Product:0x58582b0>
我可以收集产品,但一旦我尝试从中收集帖子,它就会给我错误
undefined method `posts' for #<Class:0x5a248d0>
我试过了:包括两个方向,但都无济于事
编辑:这是我的大多数模型:(我以前认为它可能会拥挤,不包括)
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"one_answers"end"都是出乎意料的。我想搜索"多标签查询",但结果没有深入到4个级别,所以这也没有多大帮助。为了方便起见,我会在表格中添加额外的列,但我想保持干燥。
你怎么想的?或者,对我来说,在谷歌上尝试什么是一个好的搜索词?非常感谢你为我提供的任何帮助。
编辑2
在此处找到一些内容,稍后将进行测试
通过多个级别的Rails关联(运气不好)
如何进行多级关联?(看起来很有希望)
看起来你在Product上缺少了一个has_many关联,你能发布那个代码吗?
它应该类似于:
class Product < ActiveRecord::Base
has_many :posts
end
经过数周的努力。。
我发现诀窍就在这块宝石上http://rubygems.org/gems/nested_has_many_through它可以做这样的事情:
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
这大大简化了我的查询和收集。