In your code you have DataMapper setup instruction
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}./prod.db")
which tells that the sqlite database-file location is in a directory named: current working directory + .
, which cannot be found and hence the error. So removing the dot from the URI should resolve your problem for that part.
Note that #{Dir.pwd}
points to the current working directory; it might be safer to point to the absolute path, in relation to the current file, by instead saying – for example: File.dirname(File.expand_path(__FILE__))
. The reason is that if the file is used outside the directory it exists in, it can be found.
Now for the class definition. In your initialization you reference a non-existing method/property called name
. I think what you are looking for is just a property declaration, such as
property :name, String
You don’t need an initializer for that, you can just use, for example, Person.create(:name => 'Foo')
which persists the object into the database.
Speaking of which, coming back to the default database connection, you might be interested using an in-memory sqlite for testing. You can do this by
DataMapper.setup(:default, "sqlite::memory:')
This will start the db from a clean slate every time you use the file.