什麼是軌道方式來查看嵌套在用戶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)
記錄儀給我集合,所以我知道代碼工作了,直到那點
#<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認爲?或者,對我來說,嘗試使用谷歌會是一個很好的搜索術語?任何幫助都非常讚賞這個noob。
編輯2
找到的東西在這裏,將測試後
Rails Associations Through Multiple Levels(沒有運氣)
How to multi-level Associations?(看起來很有希望)
對不起,我不想讓這個問題對我的所有代碼都過於冗長,但這沒有什麼幫助。 – thejonster 2011-06-07 16:40:57
在頂部的註釋代碼行中,您是否認爲'@ uc_categories'是'@ uc_products'? – 2011-06-08 06:16:22