-3

这似乎接近我需要的,但它对我不起作用。

在 Ruby 中实现 `call_user_func_array`

我有一个常量列表

TYPE = %w(:groups :projects)

我在我的相关列表模型上使用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.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'':projects'组成的数组,而不是一个符号数组。

试试这个:

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

或者,如果您很幸运并且在 Ruby 2.0.0 上运行您的应用程序,请使用符号数组字面量:

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