Where do we declare rail model attributes?

I come from the Java background, and I started learning Ruby on Rails. Consider the following code mentioned at http://guides.rubyonrails.org/active_record_basics.html

class Product < ActiveRecord::Base end 

The manual mentions that this creates a product model mapped to table products (using the pluralizable ruby ​​mechanism). It also mentions: "By doing this, you can also map the columns of each row in this table to the attributes of your model instances."

But we did not declare any attributes inside the Product product. How does he know what his attributes are?

One assumption: each table attribute is created as an attribute of the model. It's true? Then we first create the SQL table? If I later changed the table (adding new columns, say), will this also change my model dynamically?

+3
source share
4 answers

An important difference is that we are talking about ActiveRecord , i models. e. subclasses (direct and indirect) of ActiveRecord::Base , those that use their own save mechanism. For models, Rails is generally not applicable. But then again, for models other than AR, the question does not make sense :)

Each table attribute is created as a model attribute. It's true?

Yes.

Then we first create the SQL table?

. rails g model creates a model file and hyphen containing the declaration for the table behind the model. Therefore, before using your model, you must first migrate.

If I change the table later (adding new columns, say), will this also change my model dynamically?

Now it is difficult. This is certainly true if the application restarts after changes (for example, in development mode this happens from time to time), since the model class will be restored. So the answer is yes, most of the time .

This, however, is only about the internal structures of the model class (visible in eg Model.columns ) that you do not always need to worry about. When data is extracted, all columns of the result set will be mapped to the attributes of model objects. This way, it even saves custom columns that you specify in SELECT s:

 Thing.select(:id, "1 AS one").first.attributes #> SELECT "things"."id", 1 AS one FROM "things" ORDER BY "things"."id" ASC LIMIT 1 # => {"id"=>1, "one"=>1} 
+4
source

It works as follows:

 class Product < ActiveRecord::Base 

Product subclassed to ActiveRecord::Base (you know that subclasses are from Java, right?).

ActiveRecord::Base can be seen here :

Active Record objects do not directly indicate their attributes, but rather derive them from the # definition of the table with which they are associated. Adding, deleting and changing attributes #, and their type is performed directly in the database. Any changes are instantly reflected in # Active Record Objects. The mapping that binds this Active Record class to a specific # database table will happen automatically in most common cases, but can be overwritten for unusual ones.

You can read another code; In short, this means that ActiveRecord uses an SQL schema to populate the corresponding attributes.

-

Since your model is Class , ActiveRecord will basically create a series of setter/getter instance methods with values ​​from your db.

When you call the specified class, ActiveRecord::Base populates the corresponding instance methods with values ​​in your db, allowing you to call @product.name , etc.

+4
source

Models on rails are just an easy way to bind data from a database. A model is data.

The model represents the table and will have all the columns of this table as its attributes.

Model at Product . Under rails conventions, this model is directly mapped to the products table in the database and will have all the attributes that the table has as its columns.

Models and tables are interconnected, and models serve as a light abstract layer over the actual data to ensure ease of operation and additional checks, etc.

+1
source

You only need to declare certain attributes during the migration (which creates the tables). Otherwise, ActiveRecord makes some key assumptions:

 name of the table = lowercase version of class name = products primary key = id 

He can then use raw SQL when he starts the connection to get a list of attributes from the table:

 DESCRIBE table products; 

This gives a complete list of fields in the table. It sets the attributes in each instance of the class based on these fields.

0
source

Source: https://habr.com/ru/post/1242701/


All Articles