Rails:查找所有条件 [英] Rails: Find all with conditions
问题描述
在我的应用中,我有来自不同国家/地区的用户,并希望对他们进行查找
in my app I have users from different countries and want to perform finds on them
我尝试在索引操作中这样做
I tried to do it like this in the index action
@fromcanada = User.find(:all, :country => 'canada')
但是我得到了错误
Unknown key: country
然而,这让我想问,什么可以成为钥匙?在我的数据库架构文件中,我在用户表上有一个国家"列.
However, so that leads me to ask, what can become a key? In my database schema file, I have a "country" column on the users table.
t.string "country"
此外,当我找到所有内容时
Furthermore, when I did a find all
@users = User.all
我能够做到这一点
<%= user.country %></p>
你能解释为什么我的 find all with conditions 不起作用吗?告诉我我应该怎么做?
Can you explain why my find all with conditions didn't work? and show me how I should have done it?
推荐答案
试试这个.
@fromcanada = User.find(:all, :conditions => { :country => 'canada' })
正如 jason328 所指出的,上述答案在 3.2 中已弃用,更新后的答案是
edit: As jason328 pointed out, the above answer is deprecated in 3.2, and an updated answer would be
@fromcanada = User.where(:country => 'canada')
这篇关于Rails:查找所有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!