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

发布于 2025-01-03 16:50:27 字数 42 浏览 0 评论 0原文

我应该在 Rails 中的什么位置放置一个将被我的所有模型使用的方法?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

述情 2025-01-10 16:50:27

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

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

module Reusable
   def reusable_method_1
     puts "reusable"
   end

   def reusable_method_2
     puts "reusable"
   end
end

假设您想在用户模型中使用它

class User < ActiveRecord::Base
  include Reusable
end

并且还要确保为 application.rb 中的 lib/ 目录启用 autoload_path

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

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

create a file in 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

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
别想她 2025-01-10 16:50:27

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

# 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

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
夏日浅笑〃 2025-01-10 16:50:27

有多种方法可以实现此目的

  1. 使用 OOP 并在项目中为 ActiveRecord::Base 创建一个子类,并使用该类作为所有模型的父类
  2. Monkey path ActiveRecord::Base
  3. 创建一个模块并包含该模块在您所有的模型中

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. Monkey path ActiveRecord::Base
  3. Create a module and include that in all your models
陪你到最终 2025-01-10 16:50:27

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

在您执行任何操作之前,我很好奇所有模型中都需要包含哪种方法?我们正在讨论多少种模型以及您想要解决什么问题?

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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文