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: 217
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 Foo
s 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
orCountry
. (This means that my table consists of the rowsid
,name
,parent_id
,type
)
Let's say for instance, I want to query Foo
s in the state "California". Beside Foo
s that directly belong to "California", I should get all Foo
s that belong every City
in "California" like Foo
s in "Los Angeles" and "San Francisco". Not only that, but I should get any Foo
s 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