简体   繁体   English

从RoR模型中获取两个随机元素

[英]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. 我正在尝试使用RoR进行简单的操作,而我在学习基础知识时遇到了一些麻烦。 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). 我最接近的背景是ASP.NET MVC,但我发现所有的RoR教程都关注rails真正擅长什么(脚手架的东西)而不是如何制作自己的动作并让他们用参数等来做事。在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? 我想我正在处理某种类型的ActiveRecord集合?

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. 我已经读过在集合/数组的某个地方有一个.rand方法,尽管其他地方认为rand只是一种获取随机数达到一定数量的方法。 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. 任何可以帮助解决这个问题的东西,理想情况下有助于从ASP.NET MVC到RoR的进一步修补,我们将非常感激。

要从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: 如果您想要数据库中的2个随机项,请向数据库询问2个随机项:

@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. 如果您只使用2,那么从您的系统加载所有Item是毫无意义的,这是一种浪费。

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: 如果已经拥有从别的地方,你需要从随机值的数组,然后Rails添加一个rand方法Array类:

@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. 我不知道你试图用Item[0]做什么,但是在Rails中没有做任何有意义的事情。

What does your model look like? 你的模型是什么样的? I'm not sure what you're trying to do with Item[0] there. 我不确定你在那里用Item [0]做什么。 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. 然后你可以做@items [0]和@items [1]得到2项随机数组。

As for params, you can get any form variables or request params from the query string by using the params hash: 对于params,您可以使用params哈希从查询字符串中获取任何表单变量或请求参数:

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. 如果您将其作为资源路由,Rails控制器通常包含一个或多个restful操作(索引,显示,新建,创建,删除,编辑,更新),但是添加自己的操作只需向控制器添加新方法,在routes.rb中路由该操作,并使用该操作的名称创建一个视图。

使用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: 有关您的模型的更多信息以及您要完成的内容会有所帮助,但如果您尝试从sqlite等数据库中提取随机记录,则可以执行以下操作:

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

Where Items is your model class. 其中Items是您的模型类。 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. 'RANDOM()'只是一个传递给数据库的字符串,告诉它如何排序,所以你必须调整以匹配你正在使用的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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