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 829721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:55:41+00:00 2026-05-15T03:55:41+00:00

Right now I’m dealing with an issue regarding an intense acts_as_tree MySQL query via

  • 0

Right now I’m dealing with an issue regarding an intense acts_as_tree MySQL query via rails. The model I am querying is Foo. A Foo can belong to any one City, State or Country. My goal is to query Foos based on their location. My locations table is set up like so:

  • I have a table in my database called locations
  • I use a combination of acts_as_tree and polymorphic associations to store each individual location as either a City, State or Country. (This means that my table consists of the rows id, name, parent_id, type)

Let’s say for instance, I want to query Foos in the state “California”. Beside Foos that directly belong to “California”, I should get all Foos that belong every City in “California” like Foos in “Los Angeles” and “San Francisco”. Not only that, but I should get any Foos that belong to the Country that “California” is in, “United States”.

I’ve tried a few things with associations to no avail. I feel like I’m missing some super-helpful Rails-fu here. Any advice?

  • 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-15T03:55:42+00:00Added an answer on May 15, 2026 at 3:55 am

    I’m sure you’ve found an solution for this by now….

    For stuff like this I find I resort to denormalizing the grand ancestors and beyond. While its not best DB practice it makes the use cases much easier with just a little bit of overhead.

    So for example your locations table would have a city_id, state_id and country_id column and in the cases where the type is “state” the city_id is null. and for type=”country” city_id and state_id are null. Then at any point your query to find all of the children of a given node is simple you just have to know what node type you’re after.

    A simple before_save can do the denormalizing.

    belongs_to :city, :class_name => "Location", :foreign_key => :city_id
    belongs_to :state, :class_name => "Location", :foreign_key => :state_id
    belongs_to :country, :class_name => "Location", :foreign_key => :country_id
    
    #might want to throw in some validation that one of the ids is set...
    def before_save
      self.state_id = self.city.state_id if city_id && city
      self.country_id = self.state.country_id if state_id && state
    end
    

    Alternatively you may want to look at something like the nested set implementation that with the addition of two integer fields (left and right) lets you get all the subnodes for any node in the hierarchy my favourite one is http://github.com/collectiveidea/awesome_nested_set

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

Sidebar

Related Questions

right now I have a huge Solution in which we use javascript alerts via
Right now I have 7 columns in my MySQL table: monday tuesday wednesday ..
Right now, I keep all of my projects on my laptop. I'm thinking that
Right now, I'm particularly interested in reading the data from MP3 files (ID3 tags?),
Right now my ant task looks like. <javadoc sourcepath=${source} destdir=${doc}> <link href=http://java.sun.com/j2se/1.5.0/docs/api/ /> </javadoc>
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
Right now I have an SSIS package that runs every morning and gives me
Right now I'm making an extremely simple website- about 5 pages. Question is if
Right now, I have two Eclipse projects - they both use Maven 2 for

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.