class AdminUser < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
has_many :comments
end
我想在這個ADMINUSER下的每個用戶創建的每一個崗位的所有評論列表。
爲了讓每個用戶完成1條評論,所有評論都很簡單。我會用類似
comments=Comment.joins(:post).where("posts.user_id= ?",userid.to_s)
但是在這裏,我需要去更高一層。即獲得每個用戶在1個admin_user下發表的每條帖子的評論。我以前從未使用過多個連接,所以我有點困惑。
如果我可以用ActiveRecord的關聯字符串內它可能看起來像
comments=Comment.joins(:post).joins(:user).where("posts.user.admin_user.id=?",adminuser_id)
但我不能用posts.user.admin_user
我認爲,評論屬於帖子和帖子屬於用戶。這就是爲什麼。 – Santanu
閱讀問題。 –
是的,這是我正在尋找的連接。我的最終查詢是Comment.joins(post: :user).where(「user.admin_user_id =?,admin_id) –