I am pretty much new at this ROR game and had what I think to be a simple question. I have a set of Sponsors that I would like to be able to select one at random and display in the my html. I have already set up the DB, scaffolded, set index controller and all is working smoothly. I know that I can display them all by doing <% for sponsor in @sponsors %> <%= sponsor.name %> <% end %> How can I just select one of the sponsors to display each time someone hits this page? Can someone PLEASE help? Or guide me in the right direction? I am assuming it would be best for the DRY process to set this up once in my Controller/Model but I am at a complete loss on what the best way to do this is. THANKS! -- Posted via http://www.ruby-forum.com/.
there is a wonderful command for random uses, called, (surprisingly) RAND(). so, if i am correct, you can go like this: def anaction @variable = MyModel.find(:first, :order => ["RAND()"]) and it should bring forth end # the first object it finds, in a random order. or, another option i haven''t really thought through if you wanted to do this in the view (which wouldn''t be mvc, but ...) is to use the array.SORT! { |o| block } and specify the block to randomize the array. either way, one of the two should work. (first choice is probably preferable) is it helping? s -- Posted via http://www.ruby-forum.com/.
Mason Kessinger wrote: I have a set of Sponsors that I would like to be able to select one at random and display in the my html. I have already set up the DB, scaffolded, set index controller and all is working smoothly. I know that I can display them all by doing <% for sponsor in @sponsors %> <%= sponsor.name %> <% end %> How can I just select one of the sponsors to display each time someone hits this page? <%= @sponsors[(rand*(@sponsors.size-1)).round] %> or make a method in your Sponsor model to be DRYer: def self.random find(:all)[(rand*(count-1)).round] end and then you can say @sponsor = Sponsor.random or whatever. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060718/31d818d3/attachment-0001.html
Shai, Thanks for your quick reply. In my Sponsors controller i should put this: def featured_sponsor @featured = Sponsor.find(:first, :order => ["RAND()"]) end Then how, in my html can I display a) sponsor.name b) sponsor.website_url and c) sponsor.description? I see now how to connect to the data, but how can i display it? Thanks a million! -- Posted via http://www.ruby-forum.com/.
On 7/19/06, Mason Kessinger <masonkessinger@gmail.com> wrote:> > Shai, Thanks for your quick reply. > > > > In my Sponsors controller i should put this: > > > def featured_sponsor > @featured = Sponsor.find(:first, :order => ["RAND()"]) > end > > > > Then how, in my html can I displaya) sponsor.name <%=h sponsor.name %> b)> sponsor.website_url<%= link_to sponsor.name, sponsor.website_url %> and c) sponsor.description? <%=h sponsor.description I see now how to connect to the data, but how can i display it?> > > Thanks a million! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060718/645d1294/attachment.html
I guess it should rather be this in my controller. def featured_sponsor @sponsor = Sponsor.find(:first, :order => ["RAND()"]) end right? -- Posted via http://www.ruby-forum.com/.
> there is a wonderful command for random uses, called, (surprisingly) > RAND(). > > so, if i am correct, you can go like this: > > def anaction > @variable = MyModel.find(:first, :order => ["RAND()"]) and it should > bring forth > end # the first object it finds, in a random order.RAND() is a pricey option though as it''s going to select every row in your table, assign it a random value, then sort it on that value. Then return the first record. Might be a lot faster to find the maximum id, then use ruby to generate a random value between 1 and max_id, then so something like: MyModel.find(:first, :conditions => ["id >= ?", random_id]) Or something along those lines. On my end_users table which has about 80,000 rows, the rand() takes about 1.60 seconds. The second query takes 0.00... not super scientific, but still... mysql> explain select * from end_users order by rand() limit 1; +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ | 1 | SIMPLE | end_users | ALL | NULL | NULL | NULL | NULL | 80525 | Using temporary; Using filesort | +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ 1 row in set (0.00 sec) mysql> explain select * from end_users where id >= 12345 limit 1; +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ | 1 | SIMPLE | end_users | range | PRIMARY | PRIMARY | 4 | NULL | 65667 | Using where | +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+
Your second solution is more effecient, but it does make a few assumptions. We have no guarantee that all the ids between 0 and max_id actually exist. There could possibly be gaps. You could throw it in a loop and do it over if it doesn''t exist; so this isn''t a huge problem, but it is something to take into account. On 7/18/06, Philip Hallstrom <rails@philip.pjkh.com> wrote:> > > there is a wonderful command for random uses, called, (surprisingly) > > RAND(). > > > > so, if i am correct, you can go like this: > > > > def anaction > > @variable = MyModel.find(:first, :order => ["RAND()"]) and it should > > bring forth > > end # the first object it finds, in a random order. > > RAND() is a pricey option though as it''s going to select every row in your > table, assign it a random value, then sort it on that value. Then return > the first record. > > Might be a lot faster to find the maximum id, then use ruby to generate a > random value between 1 and max_id, then so something like: > > MyModel.find(:first, :conditions => ["id >= ?", random_id]) > > Or something along those lines. > > On my end_users table which has about 80,000 rows, the rand() takes about > 1.60 seconds. The second query takes 0.00... not super scientific, but > still... > > mysql> explain select * from end_users order by rand() limit 1; > > +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ > | id | select_type | table | type | possible_keys | key | key_len | > ref | rows | Extra | > > +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ > | 1 | SIMPLE | end_users | ALL | NULL | NULL | NULL | > NULL | 80525 | Using temporary; Using filesort | > > +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ > 1 row in set (0.00 sec) > > > mysql> explain select * from end_users where id >= 12345 limit 1; > > +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ > | id | select_type | table | type | possible_keys | key | key_len > | ref | rows | Extra | > > +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ > | 1 | SIMPLE | end_users | range | PRIMARY | PRIMARY | 4 > | NULL | 65667 | Using where | > > +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060718/ddfe3e64/attachment.html
I NOW HAVE THIS class SponsorsController < ApplicationController def index @random_sponsors = Sponsor.find(:first, :order => ["RAND()"]) @sponsors = Sponsor.all_sponsors @bands = Band.all_bands end end AND THIS IN MY HTML <div id="featured_sponsor"> <p class="title"><%= random_sponsor.name %></p> <p><%=h random_sponsor.description %></p> </div> AND THIS HAPPENS: undefined local variable or method `random_sponsor'' I am sorry, I really am new to this game. I appreciate everyone''s input. BTW, there will be near 12 sponsors for this project, but probably a descent idea to code for the worst... It would be lovely if we had 80,000 sponsors though! :) -- Posted via http://www.ruby-forum.com/.
is there something wrong with this?? (:first, :order => ["RAND()"]) How does it know to look for the :id???? -- Posted via http://www.ruby-forum.com/.
In your view, change both occurrences of random_sponsor to @random_sponsor. - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 18, 2006, at 10:24 AM, Mason Kessinger wrote:> I NOW HAVE THIS > > class SponsorsController < ApplicationController > > def index > @random_sponsors = Sponsor.find(:first, :order => ["RAND()"]) > @sponsors = Sponsor.all_sponsors > @bands = Band.all_bands > end > > end > > > AND THIS IN MY HTML > > <div id="featured_sponsor"> > <p class="title"><%= random_sponsor.name %></p> > <p><%=h random_sponsor.description %></p> > </div> > > > > AND THIS HAPPENS: > > > undefined local variable or method `random_sponsor'' > > > I am sorry, I really am new to this game. I appreciate everyone''s > input. > BTW, there will be near 12 sponsors for this project, but probably a > descent idea to code for the worst... It would be lovely if we had > 80,000 sponsors though! :) > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
This brings back a full record (chosen at random), including its id. - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 18, 2006, at 10:58 AM, Mason Kessinger wrote:> is there something wrong with this?? > > (:first, :order => ["RAND()"]) > > How does it know to look for the :id???? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
And change "@random_sponsors" to "@random_sponsor" (no s) On Tue, 18 Jul 2006, Dan Kohn wrote:> In your view, change both occurrences of random_sponsor to @random_sponsor. > > - dan > -- > Dan Kohn <mailto:dan@dankohn.com> > <http://www.dankohn.com/> <tel:+1-415-233-1000> > > > > On Jul 18, 2006, at 10:24 AM, Mason Kessinger wrote: > >> I NOW HAVE THIS >> >> class SponsorsController < ApplicationController >> >> def index >> @random_sponsors = Sponsor.find(:first, :order => ["RAND()"]) >> @sponsors = Sponsor.all_sponsors >> @bands = Band.all_bands >> end >> >> end >> >> >> AND THIS IN MY HTML >> >> <div id="featured_sponsor"> >> <p class="title"><%= random_sponsor.name %></p> >> <p><%=h random_sponsor.description %></p> >> </div> >> >> >> >> AND THIS HAPPENS: >> >> >> undefined local variable or method `random_sponsor'' >> >> >> I am sorry, I really am new to this game. I appreciate everyone''s input. >> BTW, there will be near 12 sponsors for this project, but probably a >> descent idea to code for the worst... It would be lovely if we had >> 80,000 sponsors though! :) >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> Your second solution is more effecient, but it does make a few assumptions. > We have no guarantee that all the ids between 0 and max_id actually exist. > There could possibly be gaps. You could throw it in a loop and do it over if > it doesn''t exist; so this isn''t a huge problem, but it is something to take > into account.True. That''s why I did the ">=". That at least gaurantees you''ll find something (unless random_id == max_id). But you''re right, I''m assuming a fairly equal distribution of primary ids... which may or may not be correct. You could also get the number of records in the table, then do a OFFSET random_id, LIMIT 1. Although COUNT isn''t always fast for certain table types...> > On 7/18/06, Philip Hallstrom <rails@philip.pjkh.com> wrote: >> >> > there is a wonderful command for random uses, called, (surprisingly) >> > RAND(). >> > >> > so, if i am correct, you can go like this: >> > >> > def anaction >> > @variable = MyModel.find(:first, :order => ["RAND()"]) and it should >> > bring forth >> > end # the first object it finds, in a random order. >> >> RAND() is a pricey option though as it''s going to select every row in your >> table, assign it a random value, then sort it on that value. Then return >> the first record. >> >> Might be a lot faster to find the maximum id, then use ruby to generate a >> random value between 1 and max_id, then so something like: >> >> MyModel.find(:first, :conditions => ["id >= ?", random_id]) >> >> Or something along those lines. >> >> On my end_users table which has about 80,000 rows, the rand() takes about >> 1.60 seconds. The second query takes 0.00... not super scientific, but >> still... >> >> mysql> explain select * from end_users order by rand() limit 1; >> >> +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ >> | id | select_type | table | type | possible_keys | key | key_len | >> ref | rows | Extra | >> >> +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ >> | 1 | SIMPLE | end_users | ALL | NULL | NULL | NULL | >> NULL | 80525 | Using temporary; Using filesort | >> >> +----+-------------+-----------+------+---------------+------+---------+------+-------+---------------------------------+ >> 1 row in set (0.00 sec) >> >> >> mysql> explain select * from end_users where id >= 12345 limit 1; >> >> +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ >> | id | select_type | table | type | possible_keys | key | key_len >> | ref | rows | Extra | >> >> +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ >> | 1 | SIMPLE | end_users | range | PRIMARY | PRIMARY | 4 >> | NULL | 65667 | Using where | >> >> +----+-------------+-----------+-------+---------------+---------+---------+------+-------+-------------+ >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >