Is dynamically generating routes from Model enum safe? I am generating routes and responding to it dynamically by looking at the enum defined in the model. Is this safe and is there more proper method?

# Place
enum post_type: [:restaurant, :bar, :chill]

# routes  
scope '/places' do
    get '/:category', to: 'places#index', constraints: { category: Regexp.new( Place.post_types.keys.join("|") ) }
  end

# places_controller
def index
    if Place.respond_to? parmas[:category]
      @places = Place.send(params[:category]).order(:created_at).limit(10)
    end
end
Place.send(params[:category])



Using raw user data with send is very dangerous, as it allows the user to call any method (params[:category] could be "delete_all" for example).
Using raw user data with responsd_to? can also be problematic, as this method exposes inner details of the class. 

Instead, compare the user data against Place.post_types. It will look like this: (Untested)

 if  Place.post_types.has_key? params[:category]
   @places = Place.where(post_types: params[:category]).order(:created_at).limit(10)
 end




Also, dynamically creating a string for a regex is unreliable:


Regexp.new( Place.post_types.keys.join("|") )



It will break if a key contains any special characters, for example.

Instead of using a regex, test the data against Place.post_types.

EDIT: Actually, it seems this contraint is unneeded as it's tested in the controller.