简体   繁体   中英

Get two random elements from a RoR model

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.

要从ActiveRecord模型中检索两个随机项:

@things = Thing.all(:order => 'RANDOM()', :limit => 2)

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 Item s 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.

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.

使用Mysql数据库使用RAND()而不是RANDOM()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM