views:

31

answers:

2
class Exercise < ActiveRecord::Base
  belongs_to :user  
end

class User < ActiveRecord::Base
  has_many :exercises
end

Why can't I find all Exercises there were created by users with the role of "admin"?

Exercise.first :include => [:user], :conditions => ['user.role = "admin"']

The error shoes that the users table is not being joined.

Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
  Exercise Load Including Associations (0.0ms)   Mysql::Error: Unknown column 'user.role' in 'where clause': SELECT `exercises`.`id` AS t0_r0, `exercises`.`title` AS t0_r1, `exercises`.`description` AS t0_r2, `exercises`.`fitness_type` AS t0_r3, `exercises`.`trackables` AS t0_r4, `exercises`.`user_id` AS t0_r5, `exercises`.`created_at` AS t0_r6, `exercises`.`updated_at` AS t0_r7, `users`.`id` AS t1_r0, `users`.`created_at` AS t1_r1, `users`.`updated_at` AS t1_r2, `users`.`first_name` AS t1_r3, `users`.`last_name` AS t1_r4, `users`.`email` AS t1_r5, `users`.`crypted_password` AS t1_r6, `users`.`password_salt` AS t1_r7, `users`.`persistence_token` AS t1_r8, `users`.`single_access_token` AS t1_r9, `users`.`perishable_token` AS t1_r10, `users`.`login_count` AS t1_r11, `users`.`failed_login_count` AS t1_r12, `users`.`last_request_at` AS t1_r13, `users`.`current_login_at` AS t1_r14, `users`.`last_login_at` AS t1_r15, `users`.`current_login_ip` AS t1_r16, `users`.`last_login_ip` AS t1_r17, `users`.`role` AS t1_r18, `users`.`avatar_file_name` AS t1_r19, `users`.`avatar_content_type` AS t1_r20, `users`.`avatar_file_size` AS t1_r21, `users`.`avatar_updated_at` AS t1_r22 FROM `exercises` LEFT OUTER JOIN `users` ON `users`.id = `exercises`.user_id WHERE (user.role = "admin") LIMIT 1
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'user.role' in 'where clause': SELECT `exercises`.`id` AS t0_r0, `exercises`.`title` AS t0_r1, `exercises`.`description` AS t0_r2, `exercises`.`fitness_type` AS t0_r3, `exercises`.`trackables` AS t0_r4, `exercises`.`user_id` AS t0_r5, `exercises`.`created_at` AS t0_r6, `exercises`.`updated_at` AS t0_r7, `users`.`id` AS t1_r0, `users`.`created_at` AS t1_r1, `users`.`updated_at` AS t1_r2, `users`.`first_name` AS t1_r3, `users`.`last_name` AS t1_r4, `users`.`email` AS t1_r5, `users`.`crypted_password` AS t1_r6, `users`.`password_salt` AS t1_r7, `users`.`persistence_token` AS t1_r8, `users`.`single_access_token` AS t1_r9, `users`.`perishable_token` AS t1_r10, `users`.`login_count` AS t1_r11, `users`.`failed_login_count` AS t1_r12, `users`.`last_request_at` AS t1_r13, `users`.`current_login_at` AS t1_r14, `users`.`last_login_at` AS t1_r15, `users`.`current_login_ip` AS t1_r16, `users`.`last_login_ip` AS t1_r17, `users`.`role` AS t1_r18, `users`.`avatar_file_name` AS t1_r19, `users`.`avatar_content_type` AS t1_r20, `users`.`avatar_file_size` AS t1_r21, `users`.`avatar_updated_at` AS t1_r22 FROM `exercises`  LEFT OUTER JOIN `users` ON `users`.id = `exercises`.user_id WHERE (user.role = "admin")  LIMIT 1
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:608:in `select'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1617:in `select_all_rows'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1395:in `find_with_associations'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1393:in `catch'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1393:in `find_with_associations'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1546:in `find_every'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1505:in `find_initial'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:613:in `find'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:623:in `first'
    from (irb):7
>> 

I know I can do this in a 2 step process by first finding all admin users then find exercises by "user_id" but want to make this into a named_scope.

+1  A: 

Your condition should have users as plural: users.role

Awgy
+4  A: 

I am pretty sure it is

Exercise.first :include => [:user], :conditions => ['users.role = "admin"']

instead of

Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
Geoff Lanotte