1

I'm building an app in Rails 2.3.18 and I'm trying to find all users who have specific attributes on their profiles. This is found for an individual user by user.profile. User profiles have columns on them like first_name, last_name, birthday, etc.

I'm trying to devise an ActiveRecord query to find all of the users who all have a certain first name, for example.

Something like:

User.find(:where => {:first_name => "Tom"})

How should I format this query?

4

1 回答 1

2

对于 Rails 2.3,您需要:

User.find(:all, :conditions => 'profiles.first_name = "Tom"', :joins => :profile)

如果你想急切加载配置文件,你可以使用:include而不是:joins像这样:

User.find(:all, :conditions => 'profiles.first_name = "Tom"', :include => :profile)

Rails 2.3 几乎将所有内容都包含在find中,因此请查看文档find以查看所有可用选项。

显然,如果您将用户输入插入到名称条件中,请务必对其进行清理。

于 2013-08-26T18:47:57.233 回答