Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:55:52+00:00 2026-05-20T01:55:52+00:00

Rails version 3.0.3, I am new to rails, but been in webdev for a

  • 0

Rails version 3.0.3, I am new to rails, but been in webdev for a long time.

I am using awesome nested set.

I have the tables “posts”, “labels”, and “labels_posts”

posts has_and_belongs_to_many labels
labels has_and_belongs_to_many posts

labels acts_as_nested_set

I have a label_id and I want to get all posts that are associated to that label and its children, all as a single ordered result set.

Let us say that I have Labels: “L1, L1.1, L1.1.1, L1.2, L2”

Given L1, and knowing that therefore I have L1, L1.1, L1.1.1, and L1.2, I would normally run the query:

select id, title
from posts
where exists (select * from labels_posts where labels_posts.post_id = posts.id and labels_posts.label_id IN ('L1', 'L1.1', 'L1.1.1', 'L1.2'))
order by created_at desc

This query would return all the posts associated with each of those labels.

So, what is the rails way to do this?


EDIT:

So, here is my controller

@label = Label.find(params[:label])
@posts = Post.all.select do |post|
  post.label_ids.include?(@label.self_and_descendants.map(&:id))
end

And here is the rails server output

Label Load (0.5ms)  SELECT "labels".* FROM "labels" WHERE ("labels"."cached_slug" = 'caribbean') LIMIT 1
Post Load (0.6ms)  SELECT "posts".* FROM "posts"
Label Load (0.2ms)  SELECT "labels".id FROM "labels" INNER JOIN "labels_posts" ON "labels".id = "labels_posts".label_id WHERE ("labels_posts".post_id = 1 )
Label Load (0.8ms)  SELECT "labels".* FROM "labels" WHERE ("labels"."lft" >= 1 AND "labels"."rgt" <= 8) ORDER BY "lft"
Label Load (0.1ms)  SELECT "labels".id FROM "labels" INNER JOIN "labels_posts" ON "labels".id = "labels_posts".label_id WHERE ("labels_posts".post_id = 2 )
CACHE (0.0ms)  SELECT "labels".* FROM "labels" WHERE ("labels"."lft" >= 1 AND "labels"."rgt" <= 8) ORDER BY "lft"

I am not sure the select method is the one that is needed.


EDIT ANSWER:

So here is the answer I arrived at

@label = Label.find(params[:label])
@posts = Post.order('posts.created_at desc').where('labels_posts.label_id IN (?)', @label.self_and_descendants.map(&:id)).includes(:labels)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:55:52+00:00Added an answer on May 20, 2026 at 1:55 am

    try

    Post.all(:conditions => "id = labels_posts.post_id AND label_posts.label_id in('L1', 'L1.1', 'L1.1.1', 'L1.2')", :include => [:label => labels_posts], :order => :created_at)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a PHP dev and I'm new to Rails but have been getting on
Im new to Ruby On Rails.Im using Ruby version 1.8.7 and Rails version 2.3.8.I
I have installed two different version of rails in same gem set. When I
I am new to rails. My rails version is 2.3.5. I found usage like:
I'm working with an older version of Rails and ActiveRecord that does not have
I have a three-level multi-nested form in Rails. The setup is like this: Projects
i've a problem with rails in latest version. I 've create a new app
I am using the carrierwave gem with Rmagick in a Rails app. I've set
All this weekend I have been trying to setup a Rails 3.0.4 app in
I'm using the Ruby oauth-plugin (version 0.4.0.pre7) to make my Rails (3.0.3) app an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.