看到帖子嵌套在用户讨价还价的几个关联模型下面是什么?
@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'和'end'都是出乎意料的。我想要搜索“多个标签查询”,但结果并没有达到4级深度,所以这也没有多大帮助。我会在表格中添加额外的列以使其变得简单,但我想保持干燥。
Whatdya认为?或者对我来说,尝试谷歌的搜索条件是什么?任何帮助都非常感谢这个菜鸟。
编辑2
在这里找到了一些东西,稍后会测试
Rails Associations Through Multiple Levels(没有运气)
How to multi-level Associations?(看起来很有希望)
答案 0 :(得分:0)
看起来你错过了关于产品的has_many关联,你可以发布该代码吗?
它应该类似于:
class Product < ActiveRecord::Base
has_many :posts
end
答案 1 :(得分:0)
经过数周的努力......
我发现诀窍就是这个宝石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
这极大地简化了我的查询和集合。