Rails在哪里放置一个方法在所有模型中使用

时间:2022-01-07 21:18:13

Where should I put a method in Rails that will be used by all of my models?

我应该在Rails中将一个方法放在我的所有模型中?

4 个解决方案

#1


11  

You can write reusable methods in a module and include in necessary models.

您可以在模块中编写可重用的方法,并包含在必要的模型中。

create a file in lib/reusable.rb

在lib / reusable.rb中创建一个文件

module Reusable
   def reusable_method_1
     puts "reusable"
   end

   def reusable_method_2
     puts "reusable"
   end
end

Lets say if you want to use this in user model

让我们说如果你想在用户模型中使用它

class User < ActiveRecord::Base
  include Reusable
end

And also ensure that the autoload_path enabled for lib/ directory in application.rb

并确保在application.rb中为lib /目录启用了autoload_path

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)

#2


3  

Active record extensions while server starts

服务器启动时的活动记录扩展

# config/initializers/core_extensions.rb
class ActiveRecord::Base
  # write ur common base code here
  def self.per_page
    @@per_page ||= 10
  end

  def self.pagination(options)
    paginate :per_page => options[:per_page] || per_page, :page => options[:page]
  end
end

#3


2  

There are multiple ways in which you could do achieve this

有多种方法可以实现这一目标

  1. Use OOP and and create a sub class for ActiveRecord::Base in your project and use that class as a parent for all your models
  2. 使用OOP并在项目中为ActiveRecord :: Base创建一个子类,并将该类用作所有模型的父类
  3. Monkey path ActiveRecord::Base
  4. 猴子路径ActiveRecord :: Base
  5. Create a module and include that in all your models
  6. 创建一个模块并将其包含在所有模型中

#4


0  

You'll want to do some research on a Rails convention called "Concerns". Here's the lowdown: Create sub-directory called concerns in your app directory. Create your module in app/concerns and include the module in all of your models. Add the path to app/concerns to your config.autoload_path in config/application.rb.

您将要对名为“Concerns”的Rails约定进行一些研究。这是下行:在app目录中创建名为Concer的子目录。在应用程序/关注点中创建您的模块,并在所有模型中包含该模块。将app / concerns的路径添加到config / application.rb中的config.autoload_path。

Before you do any of that, I'm curious as to what sort of method would need to be included in ALL models? How many models are we talking and what problem are you trying to solve?

在你做任何这些之前,我很好奇所有模型中需要包含哪种方法?我们谈了多少型号,你想解决什么问题?

#1


11  

You can write reusable methods in a module and include in necessary models.

您可以在模块中编写可重用的方法,并包含在必要的模型中。

create a file in lib/reusable.rb

在lib / reusable.rb中创建一个文件

module Reusable
   def reusable_method_1
     puts "reusable"
   end

   def reusable_method_2
     puts "reusable"
   end
end

Lets say if you want to use this in user model

让我们说如果你想在用户模型中使用它

class User < ActiveRecord::Base
  include Reusable
end

And also ensure that the autoload_path enabled for lib/ directory in application.rb

并确保在application.rb中为lib /目录启用了autoload_path

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)

#2


3  

Active record extensions while server starts

服务器启动时的活动记录扩展

# config/initializers/core_extensions.rb
class ActiveRecord::Base
  # write ur common base code here
  def self.per_page
    @@per_page ||= 10
  end

  def self.pagination(options)
    paginate :per_page => options[:per_page] || per_page, :page => options[:page]
  end
end

#3


2  

There are multiple ways in which you could do achieve this

有多种方法可以实现这一目标

  1. Use OOP and and create a sub class for ActiveRecord::Base in your project and use that class as a parent for all your models
  2. 使用OOP并在项目中为ActiveRecord :: Base创建一个子类,并将该类用作所有模型的父类
  3. Monkey path ActiveRecord::Base
  4. 猴子路径ActiveRecord :: Base
  5. Create a module and include that in all your models
  6. 创建一个模块并将其包含在所有模型中

#4


0  

You'll want to do some research on a Rails convention called "Concerns". Here's the lowdown: Create sub-directory called concerns in your app directory. Create your module in app/concerns and include the module in all of your models. Add the path to app/concerns to your config.autoload_path in config/application.rb.

您将要对名为“Concerns”的Rails约定进行一些研究。这是下行:在app目录中创建名为Concer的子目录。在应用程序/关注点中创建您的模块,并在所有模型中包含该模块。将app / concerns的路径添加到config / application.rb中的config.autoload_path。

Before you do any of that, I'm curious as to what sort of method would need to be included in ALL models? How many models are we talking and what problem are you trying to solve?

在你做任何这些之前,我很好奇所有模型中需要包含哪种方法?我们谈了多少型号,你想解决什么问题?