ruby 没有桌子的 Rails 模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14389105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:40:00  来源:igfitidea点击:

Rails Model without a table

rubyruby-on-rails-3model

提问by user993460

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google.

我想为颜色创建一个选择列表,但不想为颜色创建一个表格。我在任何地方都看到过,但在谷歌上找不到。

My question is: How can I put the colors in a model without a database table?

我的问题是:如何在没有数据库表的情况下将颜色放入模型中?

Or is there a better rails way for doing that?

或者有没有更好的方法来做到这一点?

I have seen someone putting an array or a hash directly in the model, but now I couldn't find it.

我看到有人直接在模型中放置了一个数组或一个散列,但现在我找不到了。

回答by Louis XIV

class Model

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :whatever

  validates :whatever, :presence => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

attr_accessor will create your attributes and you will create the object with initialize() and set attributes.

attr_accessor 将创建您的属性,您将使用 initialize() 和设置属性创建对象。

The method persisted will tell there is no link with the database. You can find examples like this one: http://railscasts.com/episodes/219-active-model?language=en&view=asciicast

持久化的方法会告诉没有与数据库的链接。你可以找到这样的例子:http: //railscasts.com/episodes/219-active-model?language=en&view=asciicast

Which will explain you the logic.

这将解释你的逻辑。

回答by vedant

The answers are fine for 2013 but now rails 4 has extracted all the database independent features out of ActiveRecordinto ActiveModel. Also, there's an awesome official guidefor it.

2013 年的答案很好,但现在 rails 4 已将所有与数据库无关的特征提取ActiveRecordActiveModel. 此外,还有一个很棒的官方指南

You can include as many of the modules as you want, or as little.

您可以根据需要包含尽可能多的模块,也可以包含尽可能少的模块。

As an example, you just need to include ActiveModel::Modeland you can forgo such an initializemethod:

例如,您只需要include ActiveModel::Model并且可以放弃这样的initialize方法:

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Just use:

只需使用:

attr_accessor :name, :age

回答by Scott S

The easiest answer is simply to not subclass from ActiveRecord::Base. Then you can just write your object code.

最简单的答案就是不要从 ActiveRecord::Base 子类化。然后你就可以写你的目标代码了。

回答by hkairi

If you want to have a select list (which does not evolve) you can define a method in your ApplicationHelperthat returns a list, for example:

如果你想要一个选择列表(它不会演变),你可以在你的ApplicationHelper返回列表中定义一个方法,例如:

 def my_color_list
   [
     "red",
     "green",
     "blue"
   ]
 end

回答by Itay Grudev

If the reason you need a model without an associated table is to create an abstract class real models inherit from - ActiveRecordsupports that:

如果您需要一个没有关联表的模型的原因是创建一个抽象类真实模型继承自 -ActiveRecord支持:

class ModelBase < ActiveRecord::Base
  self.abstract_class = true
end