Adam,
Sorry for the confusion. That worked. You are the man. Now, is there a
way to do the same query without knowing what fields you are going to
get in advance? It would be nice to just pass in a hash of attributes
instead of listing out the conditions. Something like:
User.find(:all, :conditions => attributes_hash, :include
=> :user_info)
When I use your method, Adam, I get:
u = User.find(:all, :conditions => ['firstname = ? and state = ?', 'Beth', 'NY'], :include => :user_info)
=> [#<User:0x4895504 ...
But if I try the way I just suggested, I get:
attributes = {:firstname => 'Beth', :state => 'NY'}
=> {:firstname=>"Beth", :state=>"NY"}
u = User.find(:all, :conditions => attributes, :include => :user_info)
ActiveRecord::StatementInvalid: RuntimeError: ERROR C42703
Mcolumn users.state does not exist
It wouldn't be too hard to generate the condition string, but a hash
would be prettier and easier. Any ideas?
Matt
In general, you need to specify the condition like:
:conditions => [ 'users.firstname = ? and info.state = ?', 'Beth', 'NY' ]
Because you could have columns with the same name:
User.find_by_firstname('Matt').update_attribute(:state, 'confusion')
If you have this kind of thing in a controller, you might find it better to push the logic into the model:
class User
def self.all_with_name_and_state(firstname, state)
find(:all, :conditions => [ 'users.firstname = ? and info.state = ?', firstname, state ],
:include => :info)
end
end
This is discussed by Jamis Buck in
http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
-Rob