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

  • SEARCH
  • Home
  • 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 6994929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:57:19+00:00 2026-05-27T19:57:19+00:00

In this project I poke at, (I am PHP dev, not RoR), there is

  • 0

In this project I poke at, (I am PHP dev, not RoR), there is this function on a modal.

  def task 
    @task ||= if search_key 
      Project.trop_fish.tasks.find(:first, :conditions => ["description like ?", "Search key: #{search_key}%"]) 
    else 
      Project.trop_fish.tasks.find(:first, :conditions => ["(name = ? OR name like ?)","#{task_name}","#{task_name} {%}"]) 
    end 
  end

So it’s trying to find a task, from the project called trop_fish.
But whats the @task at the top.

Is it, assign the result of the finds from the if block to the @task?

Is it the same as

  def task 
    if search_key 
      @task = Project.trop_fish.tasks.find(:first, :conditions => ["description like ?", "Search key: #{search_key}%"]) 
    else 
      @task = Project.trop_fish.tasks.find(:first, :conditions => ["(name = ? OR name like ?)","#{task_name}","#{task_name} {%}"]) 
    end 
  end
  • 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-27T19:57:20+00:00Added an answer on May 27, 2026 at 7:57 pm

    Almost, not quite. It is the same thing as this:

    def task 
      if search_key 
        @task ||= Project.trop_fish.tasks.find(:first, :conditions => ["description like ?", "Search key: #{search_key}%"]) 
      else 
        @task ||= Project.trop_fish.tasks.find(:first, :conditions => ["(name = ? OR name like ?)","#{task_name}","#{task_name} {%}"]) 
      end 
    end
    

    The ||= indicates that the variable will only be set to the new value if it is not already set with a different value. As some people commenting have pointed out/to put it more simply, @task will be set to the new value if it is nil or false.

    This portion of the RoR tutorial by Michael Hartl is a great explanation of the ||= operator.

    @pguardino brings up a good point in that a PHP programmer may not be familiar with the fact that if there is no explicit return statement within a method in ruby, it will return the last non-conditional statement in the method as it’s return value, so yes, @task is being returned.

    There is another bit of text in the RoR tutorial which explains why it is advantageous to use the ||= operator when returning from a method. It is useful because it means the first call to the task method will perform an operation against the database to retrieve a task, but subsequent calls to the method within the same thread will return @task without making calls to the database (since the @task variable has already been set.

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

Sidebar

Related Questions

I got this project, where there this error keeps occurring and its not on
Why isn't this project maintained anymore? I love this app, however not updating it
This project is in PHP, but is fairly language agnostic. I'm building a project
For this project I'm working on, I'm developing live on the server (not my
my project is a PHP web application. This applies to my test server (local),
This is a moot question as I'm not on this project any more, but
In this project there is going to be two .exe files: Main, in this
I have this project I'm working on (php mysql), it contains blocks of movies
i have this project where i would like to merge a function object (in
This project is the probable first step in migrating a large CMS from Classic

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.