简体   繁体   English

Rails 4:我可以第二次打开模型定义方法吗?

[英]Rails 4: Can I open up a model a second time to define a method?

I'm using the globalize gem to translate the name attribute of my Color model. 我正在使用globalize gem转换我的Color模型的name属性。 The gem seems to generate a Color::Translation ActiveRecord model, but it doesn't provide the file in app/models. gem似乎生成了Color::Translation ActiveRecord模型,但未在app / models中提供文件。

I want to add validations to this model, so I'm wondering if I can just create a file called app/models/color_translations.rb and do something like: 我想向该模型添加验证,所以我想知道是否可以创建一个名为app / models / color_translations.rb的文件并执行以下操作:

class Color::Translation < ActiveRecord::Base
  validates_presence_of :name
end

would this extend the class's functionality (which is what I want) or overwrite everything (unwanted)? 这会扩展类的功能(这是我想要的)还是覆盖所有内容(不需要的)?

This depends slightly on context. 这在某种程度上取决于上下文。 If there is a class Color::Translation then the result of loading your app/models/color_translations.rb file will be to add that validation. 如果存在Color::Translation类,则加载app/models/color_translations.rb文件的结果将是添加该验证。 However if there is no such class, then it will define a new one. 但是,如果没有这样的类,那么它将定义一个新的类。

The tricky thing in development is that classes are (in general) loaded on demand - you don't in general know what is already loaded versus what could be loaded. 开发中棘手的事情是(通常)按需加载类-通常,您不知道已经加载了什么以及可以加载了什么。 One way around this is to do 解决此问题的一种方法是

Color::Translation.class_eval do
  validates_presence_of :name
end

which will never create a new class - it will use an existing one (if necessary Rails' autoloading will kick in) but if it can't find one it will raise an error. 它将永远不会创建一个新的类-它会使用一个现有的类(如果需要,Rails的自动加载会启动),但是如果找不到一个类,则会引发错误。

The second problem you'll have is also related: if you stick this in a file in app/models how will rails know to load it if the class already exists? 您将遇到的第二个问题也与之相关:如果将其粘贴到app / models中的文件中,如果该类已经存在,rails将如何知道如何加载它?

It looks like globalizes creates these classes on the fly, so the safest place is to put this at the bottom of color.rb. 全球化似乎是在动态创建这些类,因此最安全的方法是将其放在color.rb的底部。 This also ensures that if rails reloads Color and globalize thus creates a new Color::Translation that you validation will get added to this new class too. 这还可以确保,如果rails重新加载Color并进行全球化,则将创建一个新的Color::Translation ,您的验证也将被添加到该新类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何在模型中正确定义方法以总计列? - Ruby on Rails: How do I correctly define a method in the model to total up a column? 我可以在Rails模型中定义查询吗? - Can I define a query in Rails model? 我可以在rails模型中使用define_method吗? - Can I define_method in rails models? 我在哪里为邮件中的关联模型定义该rails方法? - where do I define this rails method for an associated model in a mailer? 如何将devise与rails 4一起用于在第二个模型上存储数据? - How can I use devise with rails 4 to store data on a second model? 如何在Rufus Scheduler内部(在控制器中)从模型中调用方法,以便根据用户输入安排时间? 滑轨4 - How can I call a method from the model, inside of the Rufus Scheduler(in the controller), so that the time is scheduled based on user input? Rails 4 如何在给定时间删除模型条目(Rails)? - How can I delete an model entry at a given time (Rails)? 在模型中定义可以在控制器中访问的方法 - define method in model that can be accessed in controller 我可以在Rails模型中为计算值使用实例方法吗? - Can I use an instance method for a computed value in a rails model? 我可以在Rails 3的模型内部存储方法中的会话变量吗? - Can I store a session variable from a method inside a model in Rails 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM