ActiveRecord::Base 不带表
-
06-09-2019 - |
题
这是不久前出现的( Rails 模型属性在数据库中没有相应的列 )但看起来提到的 Rails 插件没有维护( http://agilewebdevelopment.com/plugins/activerecord_base_without_table )。没有办法用 ActiveRecord 来做到这一点吗?
如果没有,有没有办法在不使用ActiveRecord的情况下获取ActiveRecord验证规则?
当然,ActiveRecord 希望该表存在。
解决方案
这是我已经在过去使用的方法:
在应用/模型/ tableless.rb 强>
class Tableless < ActiveRecord::Base
def self.columns
@columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
sql_type.to_s, null)
end
# Override the save method to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
end
在应用/模型/ foo.rb 强>
class Foo < Tableless
column :bar, :string
validates_presence_of :bar
end
在的脚本/控制台强>
Loading development environment (Rails 2.2.2)
>> foo = Foo.new
=> #<Foo bar: nil>
>> foo.valid?
=> false
>> foo.errors
=> #<ActiveRecord::Errors:0x235b270 @errors={"bar"=>["can't be blank"]}, @base=#<Foo bar: nil>>
其他提示
验证是简单地内的ActiveRecord的模块。您是否尝试过它们混合到您的非ActiveRecord的模式?
class MyModel
include ActiveRecord::Validations
# ...
end
我认为答案越多越好,因为这是谷歌搜索“rails 3.1 models without table”时的第一个结果之一
我在不使用 ActiveRecord::Base 的情况下实现了同样的事情,同时包含 ActiveRecord::Validations
主要目标是让一切都以格式工作,下面我提供了一个示例付款,该付款不会保存在任何地方,但仍然能够使用我们都知道和喜爱的验证进行验证。
class Payment
include ActiveModel::Validations
attr_accessor :cc_number, :payment_type, :exp_mm, :exp_yy, :card_security, :first_name, :last_name, :address_1, :address_2, :city, :state, :zip_code, :home_telephone, :email, :new_record
validates_presence_of :cc_number, :payment_type, :exp_mm, :exp_yy, :card_security, :first_name, :last_name, :address_1, :address_2, :city, :state
def initialize(options = {})
if options.blank?
new_record = true
else
new_record = false
end
options.each do |key, value|
method_object = self.method((key + "=").to_sym)
method_object.call(value)
end
end
def new_record?
return new_record
end
def to_key
end
def persisted?
return false
end
end
我希望这对某人有帮助,因为我今天花了几个小时试图解决这个问题。
更新: 对于 Rails 3 来说这可以很容易地完成。在 Rails 3+ 中,您可以使用新的 ActiveModel
模块及其子模块。这现在应该可以工作:
class Tableless
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
欲了解更多信息,您可以查看 铁路广播公司 (或者 在 AsciiCasts 上阅读相关内容)关于该主题,以及这个 耶胡达·卡茨的博客文章.
旧答案如下:
您可能需要将其添加到 John Topley 在之前的评论中提出的解决方案中:
class Tableless
class << self
def table_name
self.name.tableize
end
end
end
class Foo < Tableless; end
Foo.table_name # will return "foos"
如果您需要的话,这将为您提供一个“假”表名。如果没有这个方法, Foo::table_name
将评估为“tablelesses”。
我发现这个链接其中工程BEAUTIFULLY。
只是一个除了接受的答案:
请您的子类继承父列有:
class FakeAR < ActiveRecord::Base
def self.inherited(subclass)
subclass.instance_variable_set("@columns", columns)
super
end
def self.columns
@columns ||= []
end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
# Overrides save to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
end
这是呈现的标准调用的对象,其具有搜索表单的嵌套期与开始和对象的端属性
在控制器的动作是非常简单的但它从嵌套对象在表单上加载值并在必要时重新呈现相同的值与错误消息。
作品on Rails的3.1。
该模型:
class Criteria < ActiveRecord::Base
class << self
def column_defaults
{}
end
def column_names
[]
end
end # of class methods
attr_reader :period
def initialize values
values ||= {}
@period = Period.new values[:period] || {}
super values
end
def period_attributes
@period
end
def period_attributes= new_values
@period.attributes = new_values
end
end
在控制器:
def search
@criteria = Criteria.new params[:criteria]
end
在助手:
def criteria_index_path ct, options = {}
url_for :action => :search
end
在视图:
<%= form_for @criteria do |form| %>
<%= form.fields_for :period do |prf| %>
<%= prf.text_field :beginning_as_text %>
<%= prf.text_field :end_as_text %>
<% end %>
<%= form.submit "Search" %>
<% end %>
产生[HTML:
<form action="/admin/search" id="new_criteria" method="post">
<input id="criteria_period_attributes_beginning_as_text" name="criteria[period_attributes][beginning_as_text]" type="text">
<input id="criteria_period_attributes_end_as_text" name="criteria[period_attributes][end_as_text]" type="text">
注意强>:由辅助和嵌套属性命名格式,使得对于控制器一次加载所有值这么简单提供的action属性
有是 ActiveRecord的-无表宝石。这是一个宝石创建无表格ActiveRecord的车型,所以它有验证,协会,类型的支持。它支持活动记录2.3,3.0,3.2
推荐的方法做,在滑轨3.X(使用加载ActiveModel)具有用于关联也不类型的支持。