开发者

Beginner's question on has_and_belongs_to_many

开发者 https://www.devze.com 2022-12-18 19:59 出处:网络
My models look something like this: Section has_and_belongs开发者_高级运维_to_many :blogs Blog has_and_belongs_to_many :sections

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号