-3

これは私が必要とするものに近いようですが、私にはうまくいきません。

Ruby で call_user_func_array を実現する

私は定数としてリストを持っています

TYPE = %w(:groups :projects)

私はRelatedListモデルでhttps://github.com/brainspec/enumerizeを使用しています

enumerize :list_type, in: %w(groups projects), scope: :type

したがって、github ページのドキュメントに従って、

RelatedList.type(:groups) # returns all RelatedList of type 'groups'

ただし、定数 TYPE に含まれるすべての RelatedList を取得したい

だから私は試しました

RelatedList.type(TYPE) # doesn't work
RelatedList.send(:type, TYPE) # doesn't work
RelatedList.send(:type, *TYPE) # doesn't work.

RelatedList.type は、個別の引数のリストをシンボルとして期待していますが、私が持っているのはシンボルの配列です。

どうすればそれを達成できますか?


以下の回答のDNNXリクエストに従って、ここに完全なトレースバックがあります。

NoMethodError: undefined method `value' for nil:NilClass
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/enumerize-bc01a5cb3c79/lib/enumerize/activerecord.rb:34:in `block (2 levels) in _define_scope_methods!'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/enumerize-bc01a5cb3c79/lib/enumerize/activerecord.rb:34:in `map'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/enumerize-bc01a5cb3c79/lib/enumerize/activerecord.rb:34:in `block in _define_scope_methods!'
    from (irb):2
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus/rails.rb:134:in `console'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:116:in `block in command'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:103:in `fork'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:103:in `command'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:80:in `block (3 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:80:in `fork'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:80:in `block (2 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `each'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `each'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `each'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
    from /Users/kw/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
4

1 に答える 1

3

...私が持っているのはシンボルの配列です

いいえ、違います。これTYPE = %w(:groups :projects)は、シンボルの配列ではなく、':groups'との 2 つの文字列の配列です。':projects'

代わりにこれを試してください:

TYPE = %w(groups projects)
RelatedList.type(*TYPE)

または、幸運にも Ruby 2.0.0 でアプリを実行している場合は、シンボル配列リテラルを使用します。

TYPE = %i(groups projects)
#=> [:groups, :projects]
于 2013-08-23T07:53:56.747 に答える