Rails中的类的初始化方法在哪里?

时间:2022-09-10 17:53:14

Where is the initialize method for classes created in in Rails?

在Rails中创建的类的初始化方法在哪里?

For example, I can create a new class with the following code.

例如,我可以用下面的代码创建一个新类。

rails g model User
rake db:migrate

And now I have a /models/user.rb with a User class.

现在我有了a /model /user。有一个用户类的rb。

class User < ActiveRecord::Base
end

But from studying Ruby, I though all classes need to be initialized with an initialize method.

但是通过学习Ruby,我认为所有的类都需要用一个initialize方法进行初始化。

class User
    def initialize()
    end
end

But I never see this in Rails. How does Rails get around doing this?

但是我从来没有在Rails中看到过。Rails是如何做到这一点的?

(Currently using Rails 4.)

(目前使用Rails 4。)

3 个解决方案

#1


5  

The class inherits from ActiveRecord::Base. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

类继承自ActiveRecord: Base。https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

This class includes a ton of other files. Including a module called core, which defines initialize.

这个类包含了大量其他文件。包括一个叫做core的模块,它定义了initialize。

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L312

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb L312

So it does have it, but it is hidden away.

它确实有,但是它被隐藏起来了。

#2


2  

The constructor is derived from module ActiveRecord Base class.

构造函数派生自模块ActiveRecord基类。

Documentation about initializer.

对初始化文档。

Constructor Details

构造方法详细信息

permalink #initialize(attributes = nil) ⇒ Base

永久链接#初始化(属性= nil)⇒基地

New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table – hence you can't have attributes that aren't part of the table columns.

可以将新对象实例化为空(不传递构造参数)或预先设置属性(但尚未保存)(传递带有与相关表列名称匹配的键名的散列)。在这两个实例中,有效的属性键是由关联表的列名决定的——因此您不能拥有不属于表列的属性。

#3


2  

But from studying Ruby, I though all classes need to be initialized with an initialize method.

但是通过学习Ruby,我认为所有的类都需要用一个initialize方法进行初始化。

You don't have to define an initialize method. This is a perfectly valid Ruby class:

您不必定义一个初始化方法。这是一个完全有效的Ruby类:

class Foo
end

And of course, I can create instances:

当然,我可以创建实例:

foo = Foo.new
#=> #<Foo:0x007fa814243ad8>

But Foo does have an initialize method, it was inherited from BasicObject:

但是Foo有一个初始化方法,它是从BasicObject继承的

Foo.instance_method(:initialize)
#=> #<UnboundMethod: Foo(BasicObject)#initialize>

The default implementation however does nothing, it's an empty method returning nil.

默认实现什么都不做,它是一个返回nil的空方法。

#1


5  

The class inherits from ActiveRecord::Base. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

类继承自ActiveRecord: Base。https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb

This class includes a ton of other files. Including a module called core, which defines initialize.

这个类包含了大量其他文件。包括一个叫做core的模块,它定义了initialize。

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L312

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb L312

So it does have it, but it is hidden away.

它确实有,但是它被隐藏起来了。

#2


2  

The constructor is derived from module ActiveRecord Base class.

构造函数派生自模块ActiveRecord基类。

Documentation about initializer.

对初始化文档。

Constructor Details

构造方法详细信息

permalink #initialize(attributes = nil) ⇒ Base

永久链接#初始化(属性= nil)⇒基地

New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table – hence you can't have attributes that aren't part of the table columns.

可以将新对象实例化为空(不传递构造参数)或预先设置属性(但尚未保存)(传递带有与相关表列名称匹配的键名的散列)。在这两个实例中,有效的属性键是由关联表的列名决定的——因此您不能拥有不属于表列的属性。

#3


2  

But from studying Ruby, I though all classes need to be initialized with an initialize method.

但是通过学习Ruby,我认为所有的类都需要用一个initialize方法进行初始化。

You don't have to define an initialize method. This is a perfectly valid Ruby class:

您不必定义一个初始化方法。这是一个完全有效的Ruby类:

class Foo
end

And of course, I can create instances:

当然,我可以创建实例:

foo = Foo.new
#=> #<Foo:0x007fa814243ad8>

But Foo does have an initialize method, it was inherited from BasicObject:

但是Foo有一个初始化方法,它是从BasicObject继承的

Foo.instance_method(:initialize)
#=> #<UnboundMethod: Foo(BasicObject)#initialize>

The default implementation however does nothing, it's an empty method returning nil.

默认实现什么都不做,它是一个返回nil的空方法。