开发者

Rails 3 - Undefined method on certain collections

开发者 https://www.devze.com 2023-02-16 03:18 出处:网络
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:

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 err开发者_运维问答or 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.


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}
0

精彩评论

暂无评论...
验证码 换一张
取 消