ruby on rails - ActiveRecord siblings in many-to-many relationship -
I'm working on this one degree, but I'm looking for some inputs for inquiring in siblings
Consider the following sections
class Post & lt; ActiveRecord :: Base has_many: post_categories has_many: categories, via = & gt; : Postcabins end class category & lt; ActiveRecord :: Base has_many: post_categories has_many: Posts, via = & gt; : Post_categories end
By definition, there may be several categories in a post, what do I need for this to show the "related posts" area on the site? As I mentioned earlier, I have a working version, which is just to do the following:
Post.find (id ,: include = & gt; {categories: = & Gt ;: posts}) < Any thoughts are appreciated
< P>
The only problem is that I see what you already have, it is possible that you have all the posts Do not want to return a category with a post.
@ post = post.fund param [[id] @related_posts = post.find (: all ,: joins = & gt ;: post_categories, select = & gt; "Posts.id" ,: Group = & gt; "posts.id" ,: Order = & Gt; "post_category_count desc")
This will send the first relevant post, i.e. who have the most common categories , And you can either limit the results or add paginated.
Comments
Post a Comment