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 8645571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:04+00:00 2026-06-12T12:35:04+00:00

Is it possbile to initialize an instance variable through a named scope? I want

  • 0

Is it possbile to initialize an instance variable through a named scope? I want to remember my search criteria after the initial search. See the following:

class Foo < ActiveRecord::Base    
  attr_accessor :search

  named_scope :bar, lambda {|search|
    # Do something here to set the @search variable to search
    {
      :conditions => [
        "name LIKE :search OR description LIKE :search", 
        {:search => "%#{search}%"} ]
    } 
  }

  def match
    if name.match(@search)
      :name
    elsif description.match(@search)
      :description
    end
  end
end

Alternatively, I could create a class method that calls the named scope and then iterates through the results to set the instance variable on each, but I lose the named scope chainability in that case.

  • 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-06-12T12:35:05+00:00Added an answer on June 12, 2026 at 12:35 pm

    named scopes are not instantiating any single record, until they are iterated. And they may be, as you correctly say, be chained with more scopes.

    I see two possible solutions. One relies on the database to populate a fake column with the matching value. It should be simple using a special :select option to the query (or a special argument to .select() in rails 3)

    The other is to rely on the controller. This is much easier to implement, but you have to ensure that the “match” in ruby is the same than the “LIKE” in the database. I’m speaking about collation, unicode normalized forms etc. Most probably there is at least one case in which they behave differently in both engines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to assign a value to an instance variable during an initialize
Is it possible to do the following (e.g. initialize bool array and set all
Is it possible to initialize and delete QApplication instances many times? The following is
Possible Duplicate: Should I initialize variable within constructor or outside constructor I have here
I have a class named insect child of sprite.I have created a instance of
Is it possible to add a new property to an anonymous type instance after
Is there any clean way to initialize instance variables in a Module intended to
How would I use the parameter value as the instance variable name of an
I am struggling with some strange variable behavior in a recursive instance method. Running
Why should I use constructor to initialize instance variables while it is possible at

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.