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}
source share