What is ORM as related to Ruby on Rails?
What is ORM as开发者_高级运维 it applies to Rails and what does it mean?
ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you.
Ruby on Rails uses one called ActiveRecord, and it's a really good one.
ORM allows you to do things such as:
User.find(50).contacts
Instead of manually writing a SELECT statement with JOINs, WHEREs, etc.
ORM stands for Object-Relational-Mapping. It basically means that Active Record takes data which is stored in a database table using rows and columns, which needs to be modified or retrieved by writing SQL statements (if you're using a SQL database), and it lets you interact with that data as though it was a normal Ruby object.
Example: Suppose you want to fetch an array of all the users then instead of writing any code for database connection and then writing some SQL query like SELECT * FROM users
and converting the result into an array, I can just type User.all
and Active Record gives me that array filled with User objects that I can play with as I'd like.
It doesn't really matter which type of database you're using. Active Record smooths out all the differences between those databases for you so you don't have to think about it. You focus on writing code for your application, and Active Record will think about the nitty gritty details of connecting you to your database. It also means that if you switch from one database to another, you don't actually need to change any major application code, just some configuration files.
ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you. Ruby on Rails uses one called ActiveRecord, and it's a really good one.
Active Record as an ORM Framework
Active Record gives us several mechanisms, the most important being the ability to:
> Represent models and their data.
> Represent associations between these models.
> Represent inheritance hierarchies through related models.
> Validate models before they get persisted to the database.
> Perform database operations in an object-oriented fashion.
click hear
The key advantages of ORM
maps language types e.g. integer, strings, dates to database types
parses database output for you. If it didn't, you would have to parse the byte stream that is coming out of the database yourself
cross database portability. MySQL, PostgreSQL, etc. all extend the SQL standard, and therefore will each have slightly different syntaxes for those extensions.
Furthermore, the exact ways of starting a connection and reading outputs from the output stream will be different. A good ORM allows you to write a single code that will work on several underlying databases.
精彩评论