Hello all,
I am working on a project that requires the ability to dynamically add
one-level routes based on person name and city.
So, at any given time, someone might go to:
And the app needs to be smart enough to determine whether the data
passed after / is a user name or a city name.
Each of these potential values are stored in the database, so my first
thought was to dynamically generate it in routes.rb using AR.
User.find(:all).each do |u|
map.connect “/#{u.first_and_last_name}”,
:controller=>UsersController, :action=>:detail, :id=>u.id
end
Location.find(:all).each do | loc |
map.connect “/#{loc.city}”, :controller=>LocationController,
:action=>:detail, :id=>loc.id
end
This works conceptually, but my User model uses betternestedset and
would throw an error upon server startup (acts_as_nested_tree not
defined), so I tried adding this to routes.rb:
class User < ActiveRecord::Base;end
class Location < ActiveRecord::Base;end
Voila! Except for the fact that now, the rest of the app wouldn’t
work (not exactly sure why…I presume this caused some wonkiness with
initialization, but since I’m just re-opening and not re-defining I
would think this would still work).
Anyway, so my next idea was to create a routing controller, simply
route everything one deep to it, and let it do the lookups and then
the appropriate redirect_to. However, I’m afriad of the performance
impacts of going this route. It should only add one db lookup and one
redirect, but I think the redirect would likely be expensive.
I guess I forward this to the group with hopes that someone has faced
this problem before. My questions in order would be:
- Am I going in the completely wrong direction here?
- Have any idea why betternestedset is throwing this area?
- How much overhead do you think to routing controller approach would
add. - Is there a better way?
Thanks for any help you can provide!
Thanks!
Jake