gpt4 book ai didi

ruby-on-rails - 在具有 enum_attr 的记录上调用 .all 时参数数量错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:13 25 4
gpt4 key购买 nike

MODEL1 有一个 account_type,所以使用 gem 'enumerated_attributes',我制作了这样的模型:

class MODEL1 < ActiveRecord::Base

enum_attr :account_type, %w(^live demo disabled)

def is_live?
self.account_type == :live
end
def is_not_live?
self.account_type == :demo || self.account_type == :disabled
end
end

我不明白的奇怪的事情是,当我像这样查询任意 MODEL1 的种子时(这是我在 ruby​​mine 控制台中运行 follwing 命令时的错误,但在 rake db 期间会发生同样的 2 for 1 错误:种子):

MODEL1.all.sample

MODEL1.all

我明白了:

Dealer Load (0.3ms)  SELECT "MODEL1".* FROM "MODEL1S"
ArgumentError: wrong number of arguments (2 for 1)
from /.rvm/gems/ruby-2.0.0-p0@web/gems/enumerated_attribute-0.2.16/lib/enumerated_attribute/integrations/active_record.rb:78:in `instantiate'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `block in find_by_sql'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `block in each'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `map'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `find_by_sql'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/relation.rb:585:in `exec_queries'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/relation.rb:471:in `load'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/relation.rb:220:in `to_a'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:49:in `sample'
from (irb):7
from /.rvm/gems/ruby-2.0.0-p0@web/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /.rvm/gems/ruby-2.0.0-p0@web/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'

我正在尝试播种的模型(在 rake db:seed 期间发生错误的地方)是这样的:

  model2 = Fabricate(:MODEL2, name: "Any MODEL2 Name #{n}", cost: n, MODEL1: MODEL1.all.sample)

在MODEL2模型中

belongs_to :MODEL1

在MODEL1模型中

has_many :MODEL2s

在迁移中,MODEL2

t.references :MODEL1

在迁移中,MODEL1

t.enum :account_type

如果有更简单的方法为 MODEL1 指定 account_types,请告诉我,我只需要能够说出 MODEL1.all.sample 或 MODEL1.all

最佳答案

似乎您正在使用发行说明中的​​ Rails 4:

Model.all now returns an ActiveRecord::Relation, rather than an array of records. Use Relation#to_a if you really want an array. In some specific cases, this may cause breakage when upgrading.

所以对于初学者来说,您可能希望在模型上调用 to_a。但是您确定 enumerated_attribute 准备好 rails 4 了吗?

如果您不使用 postres,我建议您只使用 validates_inclusion_of,如果您使用 postgres,请查看 https://coderwall.com/p/azi3ka

关于ruby-on-rails - 在具有 enum_attr 的记录上调用 .all 时参数数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18553013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com