Randomised variables from sql

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. :wink:

Thanks in advance,

Sebastian

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. :wink:

attr_reader :quote

and

protected

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

Thanks a lot! :slight_smile:

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
  2. http://www.rubycentral.com/book/tut_classes.html#S4

Phil


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