我们在哪里声明 Rails 模型的属性? [英] Where do we declare attributes of a rails model?

查看:47
本文介绍了我们在哪里声明 Rails 模型的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Java 背景,并且已经开始学习 Ruby on Rails.考虑在 http://guides.rubyonrails.org/active_record_basics.html 中提到的以下代码>

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

class Product < ActiveRecord::Base
end

指南提到,这会创建一个模型 Product 映射到表 products(使用 ruby​​ 的复数机制).它还提到,通过这样做,您还可以将表中每一行的列与模型实例的属性进行映射."

The guide mentions that this creates a model Product mapped to a table products (using the pluralized mechanism of ruby). It also mentions, 'By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model.'

但是我们没有在模型 Product 中声明任何属性.它如何知道它的属性是什么?

But we did not declare any attributes inside the model Product. How does it know what are its attributes?

一个假设:表的每个属性都作为模型的一个属性.这是真的吗?那么,我们是不是先创建SQL表呢?如果我稍后更改表(例如添加新列),它是否也会动态更改我的模型?

One assumption: Every attribute of table is made as an attribute of model. Is it true? Then, do we create the SQL table first? If I change the table later on (adding new columns, say) does it also change my model dynamically?

推荐答案

重要的区别在于我们谈论的是 ActiveRecord 模型,即.e.ActiveRecord::Base 的子类(直接和间接),使用其持久性机制的子类.以下内容不适用于一般的 Rails 模型.但话又说回来,对于非 AR 模型,这个问题毫无意义:)

The important distinction is that we're talking about ActiveRecord models, i. e. subclasses (direct and indirect) of ActiveRecord::Base, those that use its persistence mechanism. The following is not true for Rails models in general. But then again, for non-AR models the question makes no sense :)

表的每个属性都作为模型的一个属性.是真的吗?

Every attribute of table is made as an attribute of model. Is it true?

是的.

那么,我们先创建 SQL 表吗?

Then, do we create the SQL table first?

正是. rails g model 创建一个模型文件和一个包含表声明的迁移模型后面.所以在使用你的模型之前,你必须先运行迁移.

Exactly. rails g model creates a model file and a migration that contains a declaration for a table behind the model. So before using your model, you have to run the migration first.

如果我稍后更改表格(例如添加新列),它是否也会动态更改我的模型?

If I change the table later on (adding new columns, say) does it also change my model dynamically?

现在这很棘手.如果应用程序在更改后重新加载(例如,在开发模式中时不时会发生这种情况),那肯定是正确的,因为模型类将被重建.所以答案是是的,大部分时间.

Now that's tricky. It's most certainly true if the application is reloaded after the changes (e. g. in development mode this happens every now and then), since the model class will be reconstructed. So the answer is yes, most of the time.

然而,这只是关于模型类的内部结构(在例如 Model.columns 中可见),您不必总是关心.获取数据时,结果集的所有列都将映射到模型对象的属性.因此,这甚至可以保留您在 SELECTs 中指定的自定义列:

This is, however, only about the internal structures of the model class (visible in e. g. Model.columns) you don't always have to care about. When fetching data, all the columns of the result set will be mapped to attributes of the model objects. So this keeps even custom columns you specify in SELECTs:

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}

这篇关于我们在哪里声明 Rails 模型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆