Skip to content

Instantly share code, notes, and snippets.

@voltechs
Created July 3, 2012 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voltechs/3036725 to your computer and use it in GitHub Desktop.
Save voltechs/3036725 to your computer and use it in GitHub Desktop.
has_many :members, :through => :memberships, :source => :user, :include => { :memberships => :role }, :order => 'roles.rank'
irb(main):071:0> g = Group.find("NUCC")
Group Load (0.6ms) SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 'NUCC' LIMIT 1
=> #<Group id: "NUCC", parent_id: "NUCC", name: "...", short: "", type_id: "REGT", description: "...", avatar: "nucc.png">
irb(main):072:0> g.members
SQL (0.7ms) SELECT `users`.`id` AS t0_r0, `users`.`firstname` AS t0_r1, `users`.`lastname` AS t0_r2, `users`.`rank` AS t0_r3, `users`.`unit` AS t0_r4, `users`.`class_year` AS t0_r5, `users`.`gender` AS t0_r6, `users`.`dorm` AS t0_r7, `users`.`phone` AS t0_r8, `users`.`nubox` AS t0_r9, `users`.`email` AS t0_r10, `users`.`joined_corps` AS t0_r11, `users`.`active` AS t0_r12, `users`.`password_digest` AS t0_r13, `users`.`username` AS t0_r14, `memberships`.`user_id` AS t1_r0, `memberships`.`group_id` AS t1_r1, `memberships`.`role_id` AS t1_r2, `memberships`.`initiation` AS t1_r3, `memberships`.`expiration` AS t1_r4, `roles`.`id` AS t2_r0, `roles`.`name` AS t2_r1, `roles`.`description` AS t2_r2, `roles`.`type_id` AS t2_r3, `roles`.`rank` AS t2_r4 FROM `users` LEFT OUTER JOIN `memberships` `memberships_users` ON `memberships_users`.`user_id` = `users`.`id` AND expiration > '2012-07-02' LEFT OUTER JOIN `roles` ON `roles`.`id` = `memberships_users`.`role_id` INNER JOIN `memberships` ON `users`.`id` = `memberships`.`user_id` WHERE `memberships`.`group_id` = 'NUCC' AND (expiration > '2012-07-02') ORDER BY roles.rank
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'expiration' in where clause is ambiguous: SELECT `users`.`id` AS t0_r0, `users`.`firstname` AS t0_r1, `users`.`lastname` AS t0_r2, `users`.`rank` AS t0_r3, `users`.`unit` AS t0_r4, `users`.`class_year` AS t0_r5, `users`.`gender` AS t0_r6, `users`.`dorm` AS t0_r7, `users`.`phone` AS t0_r8, `users`.`nubox` AS t0_r9, `users`.`email` AS t0_r10, `users`.`joined_corps` AS t0_r11, `users`.`active` AS t0_r12, `users`.`password_digest` AS t0_r13, `users`.`username` AS t0_r14, `memberships`.`user_id` AS t1_r0, `memberships`.`group_id` AS t1_r1, `memberships`.`role_id` AS t1_r2, `memberships`.`initiation` AS t1_r3, `memberships`.`expiration` AS t1_r4, `roles`.`id` AS t2_r0, `roles`.`name` AS t2_r1, `roles`.`description` AS t2_r2, `roles`.`type_id` AS t2_r3, `roles`.`rank` AS t2_r4 FROM `users` LEFT OUTER JOIN `memberships` `memberships_users` ON `memberships_users`.`user_id` = `users`.`id` AND expiration > '2012-07-02' LEFT OUTER JOIN `roles` ON `roles`.`id` = `memberships_users`.`role_id` INNER JOIN `memberships` ON `users`.`id` = `memberships`.`user_id` WHERE `memberships`.`group_id` = 'NUCC' AND (expiration > '2012-07-02') ORDER BY roles.rank
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `query'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `block in execute'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /usr/local/lib/ruby/gems/1.9/gems/activesupport-3.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `execute'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/mysql2_adapter.rb:215:in `exec_query'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `select'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/relation/finder_methods.rb:210:in `find_with_associations'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/relation.rb:171:in `exec_queries'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/relation.rb:160:in `block in to_a'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/explain.rb:33:in `logging_query_plan'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/relation.rb:159:in `to_a'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/relation/finder_methods.rb:159:in `all'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/associations/has_many_through_association.rb:177:in `find_target'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/associations/collection_association.rb:343:in `load_target'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /usr/local/lib/ruby/gems/1.9/gems/activerecord-3.2.5/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
from /usr/local/lib/ruby/gems/1.9/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
from /usr/local/lib/ruby/gems/1.9/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
from /usr/local/lib/ruby/gems/1.9/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment