Rails:查找所有条件

编程入门 行业动态 更新时间:2024-10-28 20:20:02
本文介绍了Rails:查找所有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用中,我有来自不同国家/地区的用户,并希望对他们进行查找

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:查找所有条件

本文发布于:2023-07-15 10:08:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!