Ultra-grand super acts_as_tree rails query

Posted by Bloudermilk on Stack Overflow See other posts from Stack Overflow or by Bloudermilk
Published on 2010-02-27T02:21:03Z Indexed on 2010/05/29 20:02 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about ruby-on-rails