Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:44:13+00:00 2026-05-20T14:44:13+00:00

I have a search method written for my model Link. I’ve been able to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:44:14+00:00Added an answer on May 20, 2026 at 2:44 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a utility that search logfiles for exceptions and it have worked
I have written code for onClick method for Custom Dialog top of the another
I have a method search that I want to feed an input stream and
I have search in MSDN and I can't figure where are the POST parameters
I have a search form with a query builder. The builder is activated by
I have a search box that doesn't have a submit button, I need to
I have a search form in an app I'm currently developing, and I would
I have a search form on each of my pages. If I use form
I have a search program that will be looking at a database from a
I have a search query that I'm inheriting and attempting to optimize. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.