class Answer < ActiveRecord::Base
def self.energy(v, w)
a = self.where('energy_id = ? AND weight = ?', v, w)
a.offset(rand(a.count)).first.name
end
視圖
<%= form_for(@answer) do |f| %>
<%= f.submit "#{Answer.energy(3, 1)}", name: "answer", class: "btn" %>
<%= f.submit "#{Answer.energy(4, 1)}", name: "answer", class: "btn" %>
<% end %>
我有,它返回一個隨機值,正常。我打電話給這36次(18對2),而且我不希望相同的價值不止一次地返回。我嘗試了各種.pop
變化,但每次都失敗。
感謝您的幫助!
爲號召,我使用這些表單按鈕,所以我去了:
<% names = [] %>
<div id='one' class='center'>
<%= form_for(@answer) do |f| %>
<%= f.submit "#{record = Answer.energy(3, 1, names)}", name: "answer", class: "btn btn-large btn-primary" %>
<% names << record %>
<%= f.submit "#{record = Answer.energy(4, 1, names)}", name: "answer", class: "btn btn-large btn-primary" %>
<% names << record %>
<% end %>
</div>
~~ 17 more times ~~
的作品就像一個魅力!謝謝,jvnill,尋求幫助!
你調用方法'energy' 16次,或者你只是想16個唯一行? – texasbruce 2013-02-10 02:06:38
您使用的是什麼ORM? ActiveRecord的? – 2013-02-10 03:19:31
是的。 ActiveRecord :: Base – Dudo 2013-02-10 07:36:06