# Randomised variables from sql

**URL:** <https://www.ruby-forum.com/t/randomised-variables-from-sql/98320>\
**Category:** Rails\
**Created:** [January 28, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320 "2006-01-28T19:14:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [January 28, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320/1 "2006-01-28T19:14:50Z")

</div>

I’ve spent some time converting my website from php to RoR, and so far,  
I’m loving every bit of Rails. However, I’ve stumbled across a problem.

On my old page, I’ve had a random quote display at a specific location,  
and I’d like to replicate this with RoR. Basically, I thought I would be  
able to add all quotes into a specific table in the database, and  
somehow have a global variable stored in  
/app/controllers/application.rb, since I want it to be accessed from all  
different controllers. So far, I’ve managed to get this to work:

$quote = ““An witty saying proves nothing”- Voltaire”

in application.rb and

\<%= $quote %\>

in my layout file, which I gather is a step on the way. From here on,  
however, I’ve had no luck. I’ve tried setting the variable to many  
different things, but it doesn’t seem to want to take anything but pure  
text (as displayed above). I’ve tried searching for this in the docs and  
the wiki, but documentation on (any kind of) variables is amazingly  
noticable in its absence.

Basically, I would be very happy if someone could point me in the right  
direction to get the variable to somehow draw information out of the  
database. If given that much information, I’m sure I could find a way to  
randomise the quotes myself, but if someone wants to tell me how to do  
that as well, I’d be even happier. 😉

Thanks in advance,

Sebastian

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [January 29, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320/2 "2006-01-29T16:56:56Z")

</div>

Thanks, it worked! The correct syntax would be:

@quote = Quote.find(:first, :order =\> “RAND()”, :limit =\> 1)

Also, just to be a pain, I was wondering if you (or someone else) could  
explain what the following lines are, and why they’re there. I really  
want to be able to understand what it is I’m doing, so that I’ll be able  
to solve issues myself next time. 😉

> ```
> attr_reader :quote
> 
> ```

and

> ```
> protected
> ```

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [January 29, 2006, 1:46am UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320/3 "2006-01-29T01:46:34Z")

</div>

Sebastian-

```
I'll take a shot at it for you. You really don't want to use $global

```

vars in rails. Since it will run in different fcgi processes you  
won’t be sure to get the same process on subsequent requests. So you  
are better off making a before filter in your application.rb file.  
Something like this:

class ApplicationController \< …

```
before_filter :get_quote
attr_reader :quote

protected
def get_quote
   @quote = Quote.find(:first, :order => 'RAND')
end

```

end

```
Then @quote should be available to all your views and controllers

```

and will be a different random quote on each request. I am not  
entirely certain that the :order =\> 'RAND" part is exactly the right  
syntax or not so you should play with that part. But that’s the  
easiest way to get what you want.

Cheers-  
-Ezra

On Jan 28, 2006, at 10:14 AM, Sebastian Conrad wrote:

> somehow have a global variable stored in  
> in my layout file, which I gather is a step on the way. From here on,  
> direction to get the variable to somehow draw information out of the
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [January 30, 2006, 2:52am UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320/4 "2006-01-30T02:52:01Z")

</div>

Thanks a lot! 🙂

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [January 30, 2006, 12:18am UTC](https://www.ruby-forum.com/t/randomised-variables-from-sql/98320/5 "2006-01-30T00:18:45Z")

</div>

Sebastian Conrad wrote:

> and
> 
> > ```
> > protected
> > 
> > ```

attr\_reader :quote [1] creates a method that returns @quote.

protected [2] means that the method generated by attr\_reader is only  
accessible from within ApplicationController and any subclasses of  
ApplicationController.

1. [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M000762)
2. [http://www.rubycentral.com/book/tut\_classes.html#S4](http://www.rubycentral.com/book/tut_classes.html#S4)

Phil

–  
Philip R.  
[http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/) – DST-aware timezone library for Ruby
