I'm trying to create a model in a Rails 3.1rc4 application that is not based on ActiveRecord.
class Database
attr_reader :name
def initialize(connection, database_name)
@connection = connection
@name = database_name
end
def self.all
connection = Mongo::Connection.new("localhost")
connection.database_names.map { |db_name| new(connection, db_name) }.sort { |x, y| x.name <=> y.name }
end
end
I want to be able to render this a JSON in a _list.html.erb
template as follows
<script>
var databases = <%= @databases.as_json %>
</script>
The method in the application controller is as follows
def populate_databases
@databases = Database.all
end
I am trying to represent the collection of all of the database JSON to be processed by Backbone.js. However, I cannot seem to figure out the appropriate way to do this. The code above almost works, but the double-quotes in the results are encoded as "
. Also, I would like to be able to only include the @name
property and not the @connection property. Can someone please help me determine the appropriate way of coding this?