views:

324

answers:

4

Hi,

I'm trying to use RoR for something simple and I'm having some trouble picking up the basics. My closest background is ASP.NET MVC but I'm finding all of the RoR tutorials focus on what rails is really good at (scaffold stuff) but not how to make your own actions and get them to do stuff with parameters etc. (something trivial in ASP.NET MVC).

At the moment I am trying to get two random elements out of the model.

I think I'm dealing with an ActiveRecord collection of some sort?

I have read that there is a .rand method somewhere on collections/arrays, although other places suggest that rand is just a method for getting a random number up to a certain count. I can't even get the following code to work:

def index
    @items = Array.new(Item[0], Item[0])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @domain }
    end
end

Anything that can help with this, and ideally help with further patching from ASP.NET MVC to RoR would be really appreciated.

+2  A: 

What does your model look like? I'm not sure what you're trying to do with Item[0] there. For randomizing your array you could do something like this:

@items = ["item1", "item2", "item3"].sort_by {rand}

then you could just do @items[0] and @items[1] to get 2 items of the randomized array.

As for params, you can get any form variables or request params from the query string by using the params hash:

params[:user]

The symbol name is just the name of the form field or param in the query string.

Rails controllers usually contain one or more restful actions (index, show, new, create, delete, edit, update) if you've routed it as a resource, but you adding your own actions involves just adding a new method to your controller, routing that action in the routes.rb, and creating a view with with the name of that action.

Jack Chu
Thanks. Well there is like an automatically generated object thanks to the scaffold generation that represents a collection of objects from the database.
Graphain
+1  A: 

More info on your model & what you are trying to accomplish would help, but if you are trying to pull a random record from a database like sqlite, you can do something like:

@item = Items.find(:first, :order => 'RANDOM()')

Where Items is your model class. The 'RANDOM()' is just a string handed to the database to tell it how to sort, so you'll have to adjust to match whatever database you're using.

Bryan
Thanks, I just want 2 random elements from a collection of database objects (just 'item' with a string 'title'). Your method should work but seems kind of database tied rather than data structure focused right?
Graphain
+3  A: 

To retrieve two random items from an ActiveRecord model:

@things = Thing.all(:order => 'RANDOM()', :limit => 2)
robw
Awesome - spot on to my question - thanks :-)
Graphain
However, please see http://stackoverflow.com/questions/599459/get-two-random-elements-from-a-ror-model/599616#599616 for a better method
Graphain
The above answer is basically the same method as the first suggestion on the other answer. It does not load all items from the database - the :limit => 2 is passed to the SQL query to prevent this.
robw
Sure, my answer is similar to this because it's the best way to do it. I just wanted to add some more explanation :) For the record I upvoted this one too ;)
Gareth
+1  A: 

If you want 2 random items from the database, then ask the database for 2 random items:

@items = Item.find(:all, :limit => 2, :order => "RANDOM()")

There's no point loading all of the Items from your system if you're only using 2, that's a waste.

If you do already have an array from somewhere else that you need to get random values from, then Rails adds a rand method to the Array class:

@items = [my_arr.rand, my_arr.rand]

I don't know what you were trying to do with Item[0] but that doesn't do anything meaningful in Rails.

Gareth
Hey, this should probably be accepted answer but I felt like robw could use this one and they are both correct, yours is just a better way :-) Thanks muchly
Graphain