sean colquhoun
2006-Apr-03 07:49 UTC
[Rails] How should I pick a random entry from the database?
Hi guys. Total newbie here. I''ve been doing web stuff since 1996 but only began the foray into scripting last year. I haven''t got my skull completely 360 degrees around OOP yet. It''s just me here (in Japan) and there are no Ruby groups in my area (never mind any in English). I''ve got a database table - real simple: question, answer, and id fields - and I want to pick one question out randomly and output to the HTML page. Right now I can''t even get single entries out - all I can do is copy and paste from the scaffolding, so it''s every question or no questions at this point. I''m not sure if you can put the value into a variable, like ''@question'', and then print that, or if i have to do something like make @question into an intermediary object, like ''question'', and then do something like ''question.question'' to make it print out. Actually, if somebody could just tell me that, even if you don''t know how to do the random thing, it would be appreciated. Thanks! -- Posted via http://www.ruby-forum.com/.
Agnieszka Figiel
2006-Apr-03 10:29 UTC
[Rails] Re: How should I pick a random entry from the database?
sean colquhoun wrote:> Hi guys. Total newbie here. I''ve been doing web stuff since 1996 but > only began the foray into scripting last year. I haven''t got my skull > completely 360 degrees around OOP yet. It''s just me here (in Japan) and > there are no Ruby groups in my area (never mind any in English). > > I''ve got a database table - real simple: question, answer, and id fields > - and I want to pick one question out randomly and output to the HTML > page. Right now I can''t even get single entries out - all I can do is > copy and paste from the scaffolding, so it''s every question or no > questions at this point. I''m not sure if you can put the value into a > variable, like ''@question'', and then print that, or if i have to do > something like make @question into an intermediary object, like > ''question'', and then do something like ''question.question'' to make it > print out. Actually, if somebody could just tell me that, even if you > don''t know how to do the random thing, it would be appreciated. > > Thanks!Hi, To select a specific row you use Model.find(id), where model stands for the class that represents your question-answer table. So you could try this out by something like @entry = YourModel.find(id_you_know_that_exists) and in the view display the question like <%= @entry.question %> To pick a random question you could try first retrieving all entries from that table, then picking a random one from them, like entries = YourModel.find(:all) @entry = entries[rand(entries.size-1)] However, if this table is going to be huge this approach would be very inefficient, just giving you a hint for the easiest way to start with it :) -- Agnieszka -- Posted via http://www.ruby-forum.com/.
Juan LupiĆ³n
2006-Apr-03 10:52 UTC
[Rails] Re: How should I pick a random entry from the database?
> However, if this table is going to be huge this approach would be very > inefficient, just giving you a hint for the easiest way to start with itSo what would be the Rails Way of doing this for a huge table? Storing the number of available records in another table, or somewhere else? -- ---------------------------------------------------- http://sobrerailes.com
Tim Case
2006-Apr-03 11:10 UTC
[Rails] Re: How should I pick a random entry from the database?
I''m partial to the SQL approach: object.find(:all, :order => "Rand()") On 4/3/06, Juan Lupi?n <pantulis@gmail.com> wrote:> > However, if this table is going to be huge this approach would be very > > inefficient, just giving you a hint for the easiest way to start with it > > So what would be the Rails Way of doing this for a huge table? > Storing the number > of available records in another table, or somewhere else? > > -- > ---------------------------------------------------- > http://sobrerailes.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mikkel Bruun
2006-Apr-03 11:50 UTC
[Rails] Re: How should I pick a random entry from the database?
size = Model.count obj= Model.find(rand(size+)) Assuming the ID''s are sequential... On Monday, April 03, 2006, at 12:29 PM, Agnieszka Figiel wrote:>sean colquhoun wrote: >> Hi guys. Total newbie here. I''ve been doing web stuff since 1996 but >> only began the foray into scripting last year. I haven''t got my skull >> completely 360 degrees around OOP yet. It''s just me here (in Japan) and >> there are no Ruby groups in my area (never mind any in English). >> >> I''ve got a database table - real simple: question, answer, and id fields >> - and I want to pick one question out randomly and output to the HTML >> page. Right now I can''t even get single entries out - all I can do is >> copy and paste from the scaffolding, so it''s every question or no >> questions at this point. I''m not sure if you can put the value into a >> variable, like ''@question'', and then print that, or if i have to do >> something like make @question into an intermediary object, like >> ''question'', and then do something like ''question.question'' to make it >> print out. Actually, if somebody could just tell me that, even if you >> don''t know how to do the random thing, it would be appreciated. >> >> Thanks! > >Hi, > >To select a specific row you use Model.find(id), where model stands for >the class that represents your question-answer table. So you could try >this out by something like >@entry = YourModel.find(id_you_know_that_exists) >and in the view display the question like <%= @entry.question %> > >To pick a random question you could try first retrieving all entries >from that table, then picking a random one from them, like >entries = YourModel.find(:all) >@entry = entries[rand(entries.size-1)] > >However, if this table is going to be huge this approach would be very >inefficient, just giving you a hint for the easiest way to start with it >:) > >-- >Agnieszka > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsMikkel Bruun www.strongside.dk - Football Portal(DK) ting.minline.dk - Buy Old Stuff!(DK) -- Posted with http://DevLists.com. Sign up and save your mailbox.
Ben Munat
2006-Apr-03 15:26 UTC
[Rails] Re: How should I pick a random entry from the database?
Mikkel Bruun wrote:> > size = Model.count> obj= Model.find(rand(size+)) > > Assuming the ID''s are sequential...Yeah... if rows get deleted, the IDs won''t be reused so there will be gaps. This seems problematic. Sucks to pull all rows, but Tim''s approach might be safest. b> On Monday, April 03, 2006, at 12:29 PM, Agnieszka Figiel wrote: > >>sean colquhoun wrote: >> >>>Hi guys. Total newbie here. I''ve been doing web stuff since 1996 but >>>only began the foray into scripting last year. I haven''t got my skull >>>completely 360 degrees around OOP yet. It''s just me here (in Japan) and >>>there are no Ruby groups in my area (never mind any in English). >>> >>>I''ve got a database table - real simple: question, answer, and id fields >>>- and I want to pick one question out randomly and output to the HTML >>>page. Right now I can''t even get single entries out - all I can do is >>>copy and paste from the scaffolding, so it''s every question or no >>>questions at this point. I''m not sure if you can put the value into a >>>variable, like ''@question'', and then print that, or if i have to do >>>something like make @question into an intermediary object, like >>>''question'', and then do something like ''question.question'' to make it >>>print out. Actually, if somebody could just tell me that, even if you >>>don''t know how to do the random thing, it would be appreciated. >>> >>>Thanks! >> >>Hi, >> >>To select a specific row you use Model.find(id), where model stands for >>the class that represents your question-answer table. So you could try >>this out by something like >>@entry = YourModel.find(id_you_know_that_exists) >>and in the view display the question like <%= @entry.question %> >> >>To pick a random question you could try first retrieving all entries > >>from that table, then picking a random one from them, like > >>entries = YourModel.find(:all) >>@entry = entries[rand(entries.size-1)] >> >>However, if this table is going to be huge this approach would be very >>inefficient, just giving you a hint for the easiest way to start with it >>:) >> >>-- >>Agnieszka >> >>-- >>Posted via http://www.ruby-forum.com/. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > > Mikkel Bruun > > www.strongside.dk - Football Portal(DK) > ting.minline.dk - Buy Old Stuff!(DK) > > >
Stef T
2006-Apr-03 15:44 UTC
[Rails] Re: How should I pick a random entry from the database?
Hello Everyone, Well, not really knowing rails that in depth yet, but, knowing sql, you could always do something like; select name, random() from Model order by 2; (for postgresql) different database''s will have different names for the random() function (Sybase/MS-SQL would use rand() for example). However, this has distinct advantages over returning the entire result set and doing the randomization in rails (think large tables). I would be interested in seeing how to do this in rails - of course ;) Regards Stef Ben Munat wrote:> Mikkel Bruun wrote: >> >> size = Model.count > >> obj= Model.find(rand(size+)) >> >> Assuming the ID''s are sequential... > > Yeah... if rows get deleted, the IDs won''t be reused so there will be > gaps. This seems problematic. Sucks to pull all rows, but Tim''s > approach might be safest. > > b > >> On Monday, April 03, 2006, at 12:29 PM, Agnieszka Figiel wrote: >> >>> sean colquhoun wrote: >>> >>>> Hi guys. Total newbie here. I''ve been doing web stuff since 1996 but >>>> only began the foray into scripting last year. I haven''t got my skull >>>> completely 360 degrees around OOP yet. It''s just me here (in Japan) >>>> and >>>> there are no Ruby groups in my area (never mind any in English). >>>> >>>> I''ve got a database table - real simple: question, answer, and id >>>> fields >>>> - and I want to pick one question out randomly and output to the HTML >>>> page. Right now I can''t even get single entries out - all I can do is >>>> copy and paste from the scaffolding, so it''s every question or no >>>> questions at this point. I''m not sure if you can put the value into a >>>> variable, like ''@question'', and then print that, or if i have to do >>>> something like make @question into an intermediary object, like >>>> ''question'', and then do something like ''question.question'' to make it >>>> print out. Actually, if somebody could just tell me that, even if you >>>> don''t know how to do the random thing, it would be appreciated. >>>> >>>> Thanks! >>> >>> Hi, >>> >>> To select a specific row you use Model.find(id), where model stands for >>> the class that represents your question-answer table. So you could try >>> this out by something like >>> @entry = YourModel.find(id_you_know_that_exists) >>> and in the view display the question like <%= @entry.question %> >>> >>> To pick a random question you could try first retrieving all entries >> >>> from that table, then picking a random one from them, like >> >>> entries = YourModel.find(:all) >>> @entry = entries[rand(entries.size-1)] >>> >>> However, if this table is going to be huge this approach would be very >>> inefficient, just giving you a hint for the easiest way to start >>> with it >>> :) >>> >>> -- >>> Agnieszka >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> Mikkel Bruun >> >> www.strongside.dk - Football Portal(DK) >> ting.minline.dk - Buy Old Stuff!(DK) >> >> >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
sean colquhoun
2006-Apr-04 00:42 UTC
[Rails] Re: How should I pick a random entry from the database?
> To select a specific row you use Model.find(id), where model stands for > the class that represents your question-answer table. So you could try > this out by something like > @entry = YourModel.find(id_you_know_that_exists) > and in the view display the question like <%= @entry.question %> > > To pick a random question you could try first retrieving all entries > from that table, then picking a random one from them, like > entries = YourModel.find(:all) > @entry = entries[rand(entries.size-1)]> -- > AgnieszkaThanks! -- Posted via http://www.ruby-forum.com/.
So sorry to keep bothering you guys with really basic stuff, but i can''t get it to work. Here''s the function in my controller: def ask all_questions = Question.find(:all) @question = all_questions[rand(all_questions.size-1)] end ...and here''s the code in my "ask.rhtml" file: <%= @question.question %> The way I''ve got it figured out in my head is this: you put the data from the database into a variable in the controller (@question). Only it isn''t really a variable, it''s an object. But we talk about it like it''s a variable for some reason. Anyway, that object automatically takes the column names of the DB as its methods. So if I type in "@question.question", what I''m telling Rails is: put the contents of the "question" column from the single row that I''ve loaded the @question variable up with here. Am i missing a piece somewhere? It looks like it should work, but Rails keeps sending me ''nil'' errors. -- Posted via http://www.ruby-forum.com/.
It''s probably not a good idea to have a column in your table called question since this is the name of your model. Try calling the field a different name eg body then @question.body Hope it works for you On 4/4/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > So sorry to keep bothering you guys with really basic stuff, but i can''t > get it to work. Here''s the function in my controller: > > def ask > all_questions = Question.find(:all) > @question = all_questions[rand(all_questions.size-1)] > end > > ...and here''s the code in my "ask.rhtml" file: > > <%= @question.question %> > > The way I''ve got it figured out in my head is this: you put the data > from the database into a variable in the controller (@question). Only it > isn''t really a variable, it''s an object. But we talk about it like it''s > a variable for some reason. Anyway, that object automatically takes the > column names of the DB as its methods. So if I type in > "@question.question", what I''m telling Rails is: put the contents of the > "question" column from the single row that I''ve loaded the @question > variable up with here. > Am i missing a piece somewhere? It looks like it should work, but Rails > keeps sending me ''nil'' errors. > > -- > 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/20060404/e6c10936/attachment.html
Daniel ----- wrote:> It''s probably not a good idea to have a column in your table called > question > since this is the name of your model. > > Try calling the field a different name eg body > then @question.body > > Hope it works for youSorry. Didn''t work, so i tried bringing it down to the simplest level possible and working up from there, and I''m convinced that there''s some fundamental piece of information that i''m missing. Could somebody clue me in? For example, here''s the DEF in the controller. All i want it to do is print my name to the HTML file: def ask @booyah = ''sean'' end ...and in ''ask.rhtml'': <%= @booyah %> That''s it. It doesn''t get any simpler and yet I still can''t get it to work. Please, somebody kick me. -- Posted via http://www.ruby-forum.com/.
When you just point your browser to http://localhost:3000 (assuming webrik on port 3000) do you see anything? On 4/4/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > Daniel ----- wrote: > > It''s probably not a good idea to have a column in your table called > > question > > since this is the name of your model. > > > > Try calling the field a different name eg body > > then @question.body > > > > Hope it works for you > > Sorry. Didn''t work, so i tried bringing it down to the simplest level > possible and working up from there, and I''m convinced that there''s some > fundamental piece of information that i''m missing. Could somebody clue > me in? For example, here''s the DEF in the controller. All i want it to > do is print my name to the HTML file: > > def ask > @booyah = ''sean'' > end > > ...and in ''ask.rhtml'': > > <%= @booyah %> > > That''s it. It doesn''t get any simpler and yet I still can''t get it to > work. Please, somebody kick me. > > -- > 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/20060404/ebf410d0/attachment.html
Daniel ----- wrote:> When you just point your browser to > > http://localhost:3000 > (assuming webrik on port 3000) > > do you see anything?That''s exactly which port I''ve got it on, and I see the standard "You''ve got Rails running!" or something like that, so the server''s OK. When I go to /questions, which is the controller name, I get zero. Unless I put in something invalid. Then I get an error message. So at least I know that Rails is paying attention. Do I need more pieces to the puzzle, maybe? I mean, all I''ve got is a controller and a view. I''m not even using data; all I''m doing is putting a value into a variable/object and then asking for it out again; so I wouldn''t think that I would need anything like that. I even tried changing my variable names: @booyah, $booyah, and booyah all come up with nothing. I even tried treating @booyah like an array and typing @booyah[0], but I got a ''nil object'' error. -- Posted via http://www.ruby-forum.com/.
On Apr 3, 2006, at 11:38 PM, sean colquhoun wrote:> Daniel ----- wrote: >> When you just point your browser to >> >> http://localhost:3000 >> (assuming webrik on port 3000) >> >> do you see anything? > > That''s exactly which port I''ve got it on, and I see the standard > "You''ve > got Rails running!" or something like that, so the server''s OK. When I > go to /questions, which is the controller name, I get zero. Unless > I put > in something invalid. Then I get an error message. So at least I know > that Rails is paying attention. > > Do I need more pieces to the puzzle, maybe? I mean, all I''ve got is a > controller and a view. I''m not even using data; all I''m doing is > putting > a value into a variable/object and then asking for it out again; so I > wouldn''t think that I would need anything like that. I even tried > changing my variable names: @booyah, $booyah, and booyah all come up > with nothing. I even tried treating @booyah like an array and typing > @booyah[0], but I got a ''nil object'' error.I think you need questions/ask It''s controller/action -- -- Tom Mornini
> I think you need questions/ask > > It''s controller/action > > -- > -- Tom MorniniAwesome! That wasn''t exactly the problem, but you got me looking in the right place. You know what it was? I had ''ask'' set up as my index action. I did this just by modifying the scaffold, so what I got was: def index list render :action => ''ask'' end Anyway, when I was thinking about changing the index action back to list so that I could access the thing with questions/ask, i noticed that the 2nd line said ''list'' still. So I changed it to ''ask'' and it worked! Wait. I get it now. The 2nd line is the actual call; the 3rd line tells Rails to render the call. I thought that the call to render was the whole thing. Sorry to take your time, guys. -- Posted via http://www.ruby-forum.com/.
Problem here is that your controller renders your model, not the other way around. You want to call ''ask'' in your controller, which will then render ask.rhtml If your controller is named ''asdf'', you should hit http://localhost:3000/asdf/ask to see what happens when you run the ask method of the asdf controller. Regards Dave M. On 04/04/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> Daniel ----- wrote: > > It''s probably not a good idea to have a column in your table called > > question > > since this is the name of your model. > > > > Try calling the field a different name eg body > > then @question.body > > > > Hope it works for you > > Sorry. Didn''t work, so i tried bringing it down to the simplest level > possible and working up from there, and I''m convinced that there''s some > fundamental piece of information that i''m missing. Could somebody clue > me in? For example, here''s the DEF in the controller. All i want it to > do is print my name to the HTML file: > > def ask > @booyah = ''sean'' > end > > ...and in ''ask.rhtml'': > > <%= @booyah %> > > That''s it. It doesn''t get any simpler and yet I still can''t get it to > work. Please, somebody kick me. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >