Не удается присоединиться к модели при использовании принадлежащей - PullRequest
0 голосов
/ 23 июня 2010
class Exercise < ActiveRecord::Base
  belongs_to :user  
end

class User < ActiveRecord::Base
  has_many :exercises
end

Почему я не могу найти все Упражнения, которые были созданы пользователями с ролью «admin»?

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

Появляется ошибка, что таблица пользователей не присоединяется.

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
>> 

Я знаю, что могу сделать это в 2 этапа, сначала найдя всех пользователей-администраторов, затем найди упражнения по "user_id", но хочу превратить это в named_scope.

Ответы [ 2 ]

4 голосов
/ 23 июня 2010

Я почти уверен, что это

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

вместо

Exercise.first :include => [:user], :conditions => ['user.role = "admin"']
1 голос
/ 23 июня 2010

В вашем состоянии должны быть множественные пользователи: users.role

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...