A MODEL1 has an account_type, so 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 weird thing I don't understand is that when I query an arbitrary MODEL1 for seeds like this (this is the error when I run the follwing command in the rubymine console, but this same 2 for 1 error happens 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 seed (where the error occurs during rake db:seed) is as such:
model2 = Fabricate(:MODEL2, name: "Any MODEL2 Name #{n}", cost: n, MODEL1: MODEL1.all.sample)
in the MODEL2 model
belongs_to :MODEL1
and in the MODEL1 model
has_many :MODEL2s
in the migration, MODEL2
t.references :MODEL1
in the 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 be able to say MODEL1.all.sample or MODEL1.all
Seems you are using Rails 4 which from the release notes:
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.
So for starters you would want to call to_a
on the model. But are you sure enumerated_attribute is rails 4 ready?
If you're not using postres I would recommend just using validates_inclusion_of, if you're using postgres check out https://coderwall.com/p/azi3ka
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With