What is ORM as related to Ruby on Rails?

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

What is ORM as it applies to Rails and what does it mean?

回答1:

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.



回答2:

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.



回答3:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!