0

Let's say I have a model and a table called 'people'.

I run this code while using the Sqlite3 adapter:

People.create(name: "jim", age: 23)

This creates a new person:

1|jim|23|2013-10-15 16:59:24.708963|2013-10-15 16:59:24.708963

Now, if I was using PostgreSQL instead of Sqlite3, the process would be the identical, right? To create another person I'd write:

People.create(name: "alice", age: 32) 

How are they the same? Is this because they're going through 'activerecord'?

Would I be right in thinking that active record is simply a library that allows for easy CRUD with a database? Does it also know how to interact with different databases thanks to your settings in database.yml?

If an application works with sqlite3, will it work with any database, providing a future developer of the application installs the database and configures database.yml correctly?

4

1 回答 1

1

You are correct, the process would be the same to create a user via Rails for a database with Sqlite3, PostgreSQL, or any database. ActiveRecord handles all that for you, and it knows how to do this with the configuration in database.yml.

Theoretically, if your application works with Sqlite3, it will work with other databases when you change database.yml. But this is not guaranteed, because of idiosyncrasies between databases. Examples here, here, and here. You have to be careful because something might work in your first database, and then it breaks when you change it over.

For serious projects, I would recommend using the same database for development and production.

于 2013-10-15T17:45:26.560 回答