Invalid number of arguments when calling .all on an entry with enum_attr

A MODEL1 is of type account_type, therefore, using the gem "enumerated_attributes", I made the model as such:

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 

The strange thing that I do not understand is that when I request an arbitrary MODEL1 for such seeds (this is an error when I run the follwing command in the rubimine console, but the same 2 for 1 error occurs during rake db: seed):

 MODEL1.all.sample 

and

 MODEL1.all 

I get this:

 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)>' 

The model I'm trying to sow (where the error occurs during rake db: seed) is this:

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

in model MODEL2

 belongs_to :MODEL1 

and in model MODEL1

 has_many :MODEL2s 

in migration, MODEL2

 t.references :MODEL1 

in migration, MODEL1

 t.enum :account_type 

If there is an easier way to specify account_types for MODEL1, please let me know, I just need to say MODEL1.all.sample or MODEL1.all

+6
source share
1 answer

You seem to be using Rails 4, which is from the release notes:

Model.all now returns 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 a breakdown during the upgrade.

So, for starters, you would like to call to_a on the model. But are you sure the enumerated_attribute is rails 4?

If you are not using postres, I would recommend using validates_inclusion_of if you are using postgres check out https://coderwall.com/p/azi3ka

+1
source

Source: https://habr.com/ru/post/952902/


All Articles