Forum: Ruby on Rails Randomised variables from sql

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Sebastian C. (Guest)
on 2006-01-28 20:14
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,

Ezra Z. (Guest)
on 2006-01-29 02:46
(Received via mailing list)

	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

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


	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.


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

-Ezra Z.
Yakima Herald-Republic Newspaper
Sebastian C. (Guest)
on 2006-01-29 17:56
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
>     protected
Philip R. (Guest)
on 2006-01-30 01:18
(Received via mailing list)
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



Philip R. -- DST-aware timezone library for Ruby
Sebastian C. (Guest)
on 2006-01-30 03:52
Thanks a lot! :)
This topic is locked and can not be replied to.