Iván Vega Rivera
2005-Sep-30 20:53 UTC
Selecting two random rows and using the result in another controller
Hi,
Could you please help me on why this code does not work?
class Dictionary < ActiveRecord::Base
set_table_name ''dictionary''
set_primary_key ''word''
def Dictionary.get_rnd
ret = ''''
find(:all, :order => ''rand()'', :limit => 2) do
|word|
ret =+ word
end
return ret
end
end
I use Dictionary.get_rnd in a controller, but it gets an empty string.
What simple thing am I missing here?
Thanks!
- Ivan V.
Patrick Hurley
2005-Sep-30 22:20 UTC
Re: Selecting two random rows and using the result in another controller
> def Dictionary.get_rnd > ret = '''' > find(:all, :order => ''rand()'', :limit => 2) do |word| > ret =+ word > end > return ret > end > endJust looking at your code I think you want, where word is the name of the column in your table.> def Dictionary.get_rnd > ret = '''' > find(:all, :order => ''rand()'', :limit => 2).each do |row| > ret += row.word + '' '' > end > return ret > end > endOf course with these same assumptions, you might prefer:> def Dictionary.get_rnd > find(:all, :order => ''rand()'', :limit => 2).map { |r| r.word }.join('' '') > endHIH Patrick
Iván Vega Rivera
2005-Sep-30 22:40 UTC
Re: Selecting two random rows and using the result in another controller
Oh my... That''s elegant. Thanks! Patrick Hurley escribió:>> def Dictionary.get_rnd >> ret = '''' >> find(:all, :order => ''rand()'', :limit => 2) do |word| >> ret =+ word >> end >> return ret >> end >>end >> >> > >Just looking at your code I think you want, where word is the name of >the column in your table. > > > >> def Dictionary.get_rnd >> ret = '''' >> find(:all, :order => ''rand()'', :limit => 2).each do |row| >> ret += row.word + '' '' >> end >> return ret >> end >>end >> >> > >Of course with these same assumptions, you might prefer: > > > >> def Dictionary.get_rnd >> find(:all, :order => ''rand()'', :limit => 2).map { |r| r.word }.join('' '') >>end >> >> > >HIH >Patrick >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Iván Vega Rivera
2005-Sep-30 23:04 UTC
Re: Selecting two random rows and using the result in another controller
Also, would you help me with the new code?
It works, but it''s kinda ugly I think... Here it is:
class Dictionary < ActiveRecord::Base
set_table_name ''dictionary''
set_primary_key ''word''
def self.rnd
exists = true
while exists == true
short_url = find(:first, :order => "rand()").word <<
Kernel.rand(100).to_s
check = Shortcut.find(:all, :conditions => "short_url =
''" +
short_url + "''").map {|r| r.short_url}
if check.length == 0
exists = false
end
end
return short_url
end
end
Basically what it does is first it retrieves a random word from the db
and attaches a random number to it. Then it checks if there''s a row in
another table that already has a short_url column with that value, and
if so, generates a new random "short_url"...
I''m trying to create my own RubyURL, this way I can learn both Ruby and
Rails ;-)
Thanks a lot!
Patrick Hurley escribió:
>> def Dictionary.get_rnd
>> ret = ''''
>> find(:all, :order => ''rand()'', :limit => 2)
do |word|
>> ret =+ word
>> end
>> return ret
>> end
>>end
>>
>>
>
>Just looking at your code I think you want, where word is the name of
>the column in your table.
>
>
>
>> def Dictionary.get_rnd
>> ret = ''''
>> find(:all, :order => ''rand()'', :limit =>
2).each do |row|
>> ret += row.word + '' ''
>> end
>> return ret
>> end
>>end
>>
>>
>
>Of course with these same assumptions, you might prefer:
>
>
>
>> def Dictionary.get_rnd
>> find(:all, :order => ''rand()'', :limit =>
2).map { |r| r.word }.join('' '')
>>end
>>
>>
>
>HIH
>Patrick
>_______________________________________________
>Rails mailing list
>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
Stephen Waits
2005-Oct-01 04:13 UTC
Re: Selecting two random rows and using the result in another controller
On Sep 30, 2005, at 4:04 PM, Iván Vega Rivera wrote:> It works, but it''s kinda ugly I think... Here it is: > > class Dictionary < ActiveRecord::Base > set_table_name ''dictionary'' > set_primary_key ''word'' > def self.rnd > exists = true > while exists == true > short_url = find(:first, :order => "rand()").word << > Kernel.rand(100).to_s > check = Shortcut.find(:all, :conditions => "short_url = ''" + > short_url + "''").map {|r| r.short_url} > if check.length == 0 > exists = false > end > end > return short_url > end > end > > Basically what it does is first it retrieves a random word from the > db and attaches a random number to it. Then it checks if there''s a > row in another table that already has a short_url column with that > value, and if so, generates a new random "short_url"... > > I''m trying to create my own RubyURL, this way I can learn both Ruby > and Rails ;-)Ok.. here''s my stab at it.. class Dictionary < ActiveRecord::Base set_table_name ''dictionary'' set_primary_key ''word'' def self.rnd short_url = self.gen_short_url until self.validate_unique_url (short_url) short_url end private def gen_short_url find(:first, :order => "rand()").word << Kernel.rand(100).to_s end def validate_unique_url(url) Shortcut.count(:conditions => "short_url = ''" + url + "''") == 0 end end NOTE: I didn''t test this. Just typed it into email. Basic idea was to eliminate the unnecessary temporaries, make it slightly more functional, and switch to using ActiveRecord::count instead of ::find. --Steve
Iván Vega Rivera
2005-Oct-01 18:24 UTC
Re: Selecting two random rows and using the result in another controller
Thanks Stephen, your code looks much better, although it''s not working yet. Rails throws a NoMethodError: undefined method `validate_unique_url'' for Dictionary:Class I tried some modifications, but I couldn''t get it to work... Could you help me out? Regards, Ivan V. Stephen Waits escribió:> Ok.. here''s my stab at it.. > > class Dictionary < ActiveRecord::Base > set_table_name ''dictionary'' > set_primary_key ''word'' > > def self.rnd > short_url = self.gen_short_url until self.validate_unique_url > (short_url) > short_url > end > > private > > def gen_short_url > find(:first, :order => "rand()").word << Kernel.rand(100).to_s > end > > def validate_unique_url(url) > Shortcut.count(:conditions => "short_url = ''" + url + "''") == 0 > end > end > > NOTE: I didn''t test this. Just typed it into email. Basic idea was > to eliminate the unnecessary temporaries, make it slightly more > functional, and switch to using ActiveRecord::count instead of ::find. > > --Steve > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Patrick Hurley
2005-Oct-03 16:31 UTC
Re: Selecting two random rows and using the result in another controller
On 9/30/05, Iván Vega Rivera <ivanvr-Xl95p0XkWPRBDgjK7y7TUQ@public.gmane.org> wrote:> Also, would you help me with the new code?I will give it a try: def self.rnd loop do short_url = find(:first, :order => "rand()").word << Kernel.rand(100).to_s return short_url if Shortcut.count(["short_url = ?", short_url]) == 0 end end> Thanks a lot!Not a problem
Iván Vega Rivera
2005-Oct-03 18:11 UTC
Re: Selecting two random rows and using the result in another controller
That works! Thanks! Patrick Hurley escribió:>On 9/30/05, Iván Vega Rivera <ivanvr-Xl95p0XkWPRBDgjK7y7TUQ@public.gmane.org> wrote: > > >>Also, would you help me with the new code? >> >> > >I will give it a try: > >def self.rnd > loop do > short_url = find(:first, :order => "rand()").word << Kernel.rand(100).to_s > return short_url if Shortcut.count(["short_url = ?", short_url]) == 0 > end >end > > > >>Thanks a lot! >> >> >Not a problem >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Patrick Hurley
2005-Oct-03 18:33 UTC
Re: Selecting two random rows and using the result in another controller
On 10/3/05, Iván Vega Rivera <ivanvr-Xl95p0XkWPRBDgjK7y7TUQ@public.gmane.org> wrote:> That works! Thanks!Glad to help, I like smaller more elegant code :-), of course no matter what you write, try to remember to use the built in sql quoting when building conditions, doing it yourself will sooner or later lead to SQL injection problems, which may ruin your whole day. pth