# Using variables within a string - The function doesn't pass

**URL:** <https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501>\
**Category:** Rails\
**Created:** [January 26, 2006, 9:51am UTC](https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501 "2006-01-26T09:51:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [January 26, 2006, 9:51am UTC](https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501/1 "2006-01-26T09:51:19Z")

</div>

Hello everyone,

As a beginner to ROR, I am very confused regarding why the following  
is happening.

If my function is defined as

```
  def self.home_categories (portal_id)
      find(:all, :conditions => ["portal_id=?", portal_id] )
  end

```

the following doesn’t works (the database is queried with a value of  
0.

```
  portal_id=1
  @categories = Category.home_categories (:portal_id)

```

but this works

```
  @categories = Category.home_categories (1)

```

However,

If my function is defined as

```
  def self.home_categories (portal_id)
      find(:all, :conditions => "portal_id=#{portal_id}" )
  end

```

then the following works

```
  portal_id=1
  @categories = Category.home_categories (portal_id)

```

and the following also works:

```
  @categories = Category.home_categories (1)

```

Why is that happening? Any pointers / explanation is appreciated.

Thanks  
Frank

P.S. Thank you Ezra for your assistance with my earlier question and  
clarifying how I accidentally hijacked the thread. My sincerest  
apologies to the list members and the thread owner for the accidental  
hijack.

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [January 26, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501/2 "2006-01-26T12:38:10Z")

</div>

softwareengineer 99 wrote:

> Hello everyone,
> 
> ```
> portal_id=1
> @categories = Category.home_categories (:portal_id)
> 
> ```

portal\_id is a variable, :portal\_id is a symbol…they dont mix…

use:

```
   portal_id=1
   @categories = Category.home_categories (portal_id)

```

Mikkel

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 24, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501/3 "2006-02-24T18:45:57Z")

</div>

Hi,

From what I can see of your code I think there is just a small typo.  
You are using a symbol instead of a var.

portal\_id=1  
@categories = Category.home\_categories (:portal\_id)

should be:

portal\_id=1  
@categories = Category.home\_categories (portal\_id)

That’s all that I can see. Other than that it should work.

Eric

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 24, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/using-variables-within-a-string-the-function-doesnt-pass/97501/4 "2006-02-24T18:45:57Z")

</div>

Thanks Eric,

That totally works.

I thought I had read in the “Agile ROR” book to use symbols but  
obviously I was wrong.

Long day for me.

Thanks for the rescue again.

Frank
