Forum: Ruby on Rails Randomised variables from sql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
35544c54308ea4e0cf62e28a9782afc7?d=identicon&s=25 Sebastian Conrad (chidori)
on 2006-01-28 19: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,

Sebastian
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-29 01:46
(Received via mailing list)
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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
35544c54308ea4e0cf62e28a9782afc7?d=identicon&s=25 Sebastian Conrad (chidori)
on 2006-01-29 16: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
and
>     protected
9ab183bb240ffd3a3966d5a615b4bdeb?d=identicon&s=25 Philip Ross (Guest)
on 2006-01-30 00: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
ApplicationController.

   1. http://www.ruby-doc.org/core/classes/Module.html#M000762
   2. http://www.rubycentral.com/book/tut_classes.html#S4

Phil

--
Philip Ross
http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
35544c54308ea4e0cf62e28a9782afc7?d=identicon&s=25 Sebastian Conrad (chidori)
on 2006-01-30 02:52
Thanks a lot! :)
This topic is locked and can not be replied to.