2

I have a search method written for my model Link.

I've been able to called this method without error until implementing voting. For example, these all work:

Link.search(params[:search])
current_user.links.search(params[:search])
current_account.links.search(params[:search])

The following does not work:

@links = current_user.votes.collect {|vote| vote.voteable}
@favorites = @links.search(params[:search])

and return this error:

 undefined method `search' for #<Array:0x00000006919ac8>

I've done some testing, to see if my class is wrong, in the console:

links = user.votes.map {|vote| vote.voteable}
links.class
 => Array 
links.first.class
 => Link

This should be no different than my working examples:

user.links.class
 => Array
user.links.first.class
 => Link

I thought maybe the error was from me calling search on an array and not a link. But in previous examples I'm also calling it on an array.

I'm using vote_fu to handle the voting thus the vote/voteable.

1 Answer 1

8

The search function or scope that you have defined is defined on the Link object and is usable in Link relations, but it is not defined on a simple array, which is what is getting returned from the first collect example. Here is a simple distinction:

class User
  scope :search, lambda{ |name| where(name: name) }
end

User.search('Kombo').all # Returns an array of the SQL result run against the DB
User.all.search('Kombo') # NoMethodError: undefined method `search' for #<Array:0x000001079b15b0>

In your first example, Link.search(params[:search]), you are performing the equivalent of User.search.all, and User is a scoped ActiveRecord relation/object, which means it can continue to be combined with other scopes, like where, limit and group. In the second example, @links = current_user.votes.collect {|vote| vote.voteable}, collect is acting on such a relation and is returning a simple array which can no longer be acted upon with these scoped functions. The second example is like doing User.all.search.

It's confusing because both of these examples resolve to an Array eventually, but the difference is what is happening before that resolution to an Array, and when you are actually calling the search function. To get around this you'll have to actually call the search scope or function on an ActiveRecord object, like Link or an ActiveRecord Relation like current_user.links, but you won't be able to call it on a result. Just to clarify:

Link.search(params[:search]) # will work
Link.all.search(params[:search]) # will not work

current_user.links.search(params[:search]) # will work
current_user.links.all.search(params[:search]) # will not work

current_account.links.search(params[:search]) # will work
current_account.links.all.search(params[:search]) # will not work

When you call .collect you are implicitly calling .all, which breaks the scope chain. The following two commands are equivalent in that respect:

@links = current_user.votes.collect {|vote| vote.voteable}
@links = current_user.votes.all.collect {|vote| vote.voteable}
3
  • That makes sense Pan. I really appreciate the help this afternoon. I thought maybe something like this was the case, but confused myself with my testing in the console when I yielded the same responses with .class. I truly appreciate it, I think with that bit of knowledge I'll be able to get this thing working :). Great understanding on your part.
    – Kombo
    Mar 11, 2011 at 21:59
  • For anyone that finds this and needs the full answer. I took what Pan said, found one collection from the votes, and another collection using links.search. I then intersected the two arrays.
    – Kombo
    Mar 11, 2011 at 22:24
  • This doesn't seem to be entirely true because I'm looking at a codebase where they are calling something similar to Link.all.search(params[:search]). And the type before running the scope method is type Array. Any ideas as to how that works? I'm having to extract parts of the codebase from Rails and use some of the functionality from the models that have defined scopes. I am aware that it should be an ActiveRecord::Relation but that is not the type.
    – Petesta
    Aug 22, 2014 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.