1

My model has this scope

  scope :supported, order("name").collect {|m| m.name}.join(", ") 

and it throws an error

NoMethodError: undefined method `includes_values' for "blah, blahblah":String

I'm thinking that it's because I'm trying to return a string as an ActiveRecord object, thoughts on how to fix it? Actually I have this code already working in the view but thought it might be better in the model, maybe not?

EDIT Moving it into a non-scope class method works

def supported
  order("name").collect {|m| m.name}.join(", ") 
end

Here's a related question that better clarifies the difference between scope vs. self class methods.

Community
  • 1
  • 1
Johnny Klassy
  • 1,650
  • 5
  • 16
  • 22

1 Answers1

2

what are you trying to do exactly?, if you want to return a string, use class methods. if you want to define a chainable ARel scope, well i'd always recommend to use class methods too, but some prefer the "explicit" way via scope.

def self.supported
  order('name').to_a * ", "
end
Marian Theisen
  • 6,100
  • 29
  • 39
  • I'm trying to return a string and I thought `scope` methods does just that but then I get the error. – Johnny Klassy Sep 30 '11 at 18:00
  • 1
    no scopes are basically to prepare database queries. they are chainable, and will perform the query if you try to use array methods on them, but they expect always a ARel object in the definition, and will always return one on call. – Marian Theisen Sep 30 '11 at 18:03
  • In Rails 3, scopes can also be defined as class methods. The advantage being the return values from those methods need not be ARel objects. – Sathish Sep 30 '11 at 18:04
  • Huh Rails API doc is confusing in this regards. Anyway, I had to wrap my code above as you suggested in a class method and it works. I couldn't use your exact code since I don't want to return the whole object as string, just the name attribute. – Johnny Klassy Sep 30 '11 at 18:31