Một MODEL1 có ACCOUNT_TYPE, vì vậy sử dụng đá quý 'enumerated_attributes', tôi đã thực hiện mô hình như vậy:Sai số của đối số khi gọi .all về kỷ lục đó đã enum_attr
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
Điều lạ I don' Tôi hiểu rằng khi tôi truy vấn MODEL1 tùy ý cho các hạt giống như thế này (đây là lỗi khi tôi chạy lệnh follwing trong bảng điều khiển rubymine, nhưng lỗi 2 cho 1 này xảy ra trong khi rake db: seed):
MODEL1.all.sample
AND
MODEL1.all
tôi có được điều này:
Dealer Load (0.3ms) SELECT "MODEL1".* FROM "MODEL1S"
ArgumentError: wrong number of arguments (2 for 1)
from /.rvm/gems/[email protected]/gems/enumerated_attribute-0.2.16/lib/enumerated_attribute/integrations/active_record.rb:78:in `instantiate'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `block in find_by_sql'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `block in each'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `map'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `find_by_sql'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation.rb:585:in `exec_queries'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation.rb:471:in `load'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation.rb:220:in `to_a'
from /.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:49:in `sample'
from (irb):7
from /.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
Mô hình tôi đang cố gắng để gieo rắc (nơi các lỗi xảy ra trong quá rake db: hạt giống) là như vậy:
model2 = Fabricate(:MODEL2, name: "Any MODEL2 Name #{n}", cost: n, MODEL1: MODEL1.all.sample)
trong mô hình MODEL2
belongs_to :MODEL1
và trong MODEL1 model
has_many :MODEL2s
trong di chuyển, MODEL2
t.references :MODEL1
trong di chuyển, MODEL1
t.enum :account_type
Nếu có một cách dễ dàng hơn để xác định account_types cho MODEL1, xin vui lòng cho tôi biết, tôi chỉ cần có thể nói MODEL1.all.sample hoặc MODEL1.all
tôi nhìn vào mã nguồn ở phía trên cùng của stack trace của bạn (xem https: // github .com/jeffp/enumerated_attribute/blob/84221b872f7f71152994b212520c833742a263e3/lib/enumerated_attribute/integrations/active_record.rb) và thực sự có một cuộc gọi phương thức với hai đối số ở dòng 78, nhưng tôi không thể tìm ra cho cuộc sống của tôi như thế nào phương thức được gọi ('write_enumerated_attribute') có thể đã được định nghĩa với một phương thức. –
Có dễ dàng hơn khi xác định account_type khác với MODEL1 không? Tôi cảm thấy như nó phải là một thuộc tính liệt kê, nhưng nếu có một giải pháp thích hợp không mang lại vấn đề này, tôi rất vui khi thử? – JPEasy
Đá quý của bạn là 0.2.16 trong khi trên Github nó là 0.3.1beta. Có thể thử nâng cấp đá quý của bạn? – j03w