Beginner's question on has_and_belongs_to_many
My models look something like this:
Section
has_and_belongs开发者_高级运维_to_many :blogs
Blog
has_and_belongs_to_many :sections
has_many :posts
Post
belongs_to :blog
I can get all Posts from a certain Section's Blog by doing this:
section.blogs[n].posts
My question though is how to get all Posts connected to a Section (via a Blog)? I e something like:
section.blogs.posts
or event sweeter would be:
section.posts
Thank you!
You want something like:
Section
has_and_belongs_to_many :blogs
has_many :posts, :through => :blogs
I'm not certain the syntax is exactly correct, but the through attribute is what you are looking for.
精彩评论