Skip to content

Instantly share code, notes, and snippets.

@Fonsan
Created May 13, 2011 08:57
Show Gist options
  • Save Fonsan/970227 to your computer and use it in GitHub Desktop.
Save Fonsan/970227 to your computer and use it in GitHub Desktop.
Has many through 3.1 beta
(rdb:1) pp o
#<Arel::Nodes::SelectCore:0x000001056b0638
@groups=[],
@having=nil,
@projections=
[#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x000001056aea68
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions",
@primary_key=nil,
@table_alias=nil>,
name="*">],
@set_quantifier=nil,
@source=
#<Arel::Nodes::JoinSource:0x000001056b05c0
@left=
#<Arel::Table:0x00000105610c00
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions",
@primary_key=nil,
@table_alias=nil>,
@right=
[#<Arel::Nodes::InnerJoin:0x000001056af2d8
@left=
#<Arel::Table:0x000001056af850
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions_has_sections",
@primary_key=nil,
@table_alias=nil>,
@right=
#<Arel::Nodes::On:0x000001056af300
@expr=
#<Arel::Nodes::Equality:0x000001056af328
@left=
#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x000001056af850
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions_has_sections",
@primary_key=nil,
@table_alias=nil>,
name="question_id">,
@right=
#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x000001056af468
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions",
@primary_key=nil,
@table_alias=nil>,
name="id">>>>,
#<Arel::Nodes::InnerJoin:0x00000105632fa8
@left=
#<Arel::Table:0x0000010562c400
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions_has_sections",
@primary_key=nil,
@table_alias=nil>,
@right=
#<Arel::Nodes::On:0x00000105631b58
@expr=
#<Arel::Nodes::Equality:0x00000105631900
@left=
#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x000001055f8d58
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions",
@primary_key=nil,
@table_alias=nil>,
name="id">,
@right=
#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x0000010562c400
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions_has_sections",
@primary_key=nil,
@table_alias=nil>,
name="question_id">>>>]>,
@top=nil,
@wheres=
[#<Arel::Nodes::And:0x000001056aeba8
@children=
[#<Arel::Nodes::Equality:0x0000010563d548
@left=
#<struct Arel::Attributes::Attribute
relation=
#<Arel::Table:0x0000010562c400
@aliases=[],
@columns=nil,
@engine=ActiveRecord::Base,
@name="questions_has_sections",
@primary_key=nil,
@table_alias=nil>,
name="section_id">,
@right=1>]>]>
(rdb:1) list
[125, 134] in /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/arel-2.1.0/lib/arel/visitors/to_sql.rb
125 [
126 "SELECT",
127 (visit(o.top) if o.top),
128 (visit(o.set_quantifier) if o.set_quantifier),
129 ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?),
=> 130 (visit(o.source) if o.source),
131 ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
132 ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?),
133 (visit(o.having) if o.having),
134 ].compact.join ' '
(rdb:1)
(rdb:1) p target_scope.to_sql
"SELECT \"questions\".* FROM \"questions\" INNER JOIN \"questions_has_sections\" ON \"questions_has_sections\".\"question_id\" = \"questions\".\"id\""
(rdb:1) p @associaction_scope
nil
(rdb:1) list
[82, 91] in /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/bundler/gems/rails-2a98fd96aa2c/activerecord/lib/active_record/associations/association.rb
82 @target = target
83 loaded!
84 end
85
86 def scoped
=> 87 target_scope.merge(@association_scope)
88 end
89
90 # Construct the scope for this association.
91 #
(rdb:1) p target_scope.merge(@association_scope).to_sql
"SELECT \"questions\".* FROM \"questions\" INNER JOIN \"questions_has_sections\" ON \"questions_has_sections\".\"question_id\" = \"questions\".\"id\" INNER JOIN \"questions_has_sections\" ON \"questions\".\"id\" = \"questions_has_sections\".\"question_id\" WHERE \"questions_has_sections\".\"section_id\" = 1"
(rdb:1) p target_scope.method[:merge]
ArgumentError Exception: wrong number of arguments(0 for 1)
(rdb:1) p target_scope.method(:merge)
#<Method: ActiveRecord::Relation(Squeel::Adapters::ActiveRecord::Relation)#merge>
(rdb:1)
class Question < ActiveRecord::Base
has_many :questions_has_sections
has_many :sections, :through => :questions_has_sections
end
# coding: utf-8
class QuestionsHasSection < ActiveRecord::Base
belongs_to :question
belongs_to :section
end
class Section < ActiveRecord::Base
has_many :questions_has_sections
has_many :questions, :through => :questions_has_sections
end
$ rails c
Loading development environment (Rails 3.1.0.beta1)
dok(dev)> Section.first.questions
Section Load (0.3ms) SELECT `sections`.* FROM `sections` LIMIT 1
Question Load (0.2ms) SELECT `questions`.* FROM `questions` INNER JOIN `questions_has_sections` ON `questions_has_sections`.`question_id` = `questions`.`id` INNER JOIN `questions_has_sections` ON `questions`.`id` = `questions_has_sections`.`question_id` WHERE `questions_has_sections`.`section_id` = 1
Mysql2::Error: Not unique table/alias: 'questions_has_sections': SELECT `questions`.* FROM `questions` INNER JOIN `questions_has_sections` ON `questions_has_sections`.`question_id` = `questions`.`id` INNER JOIN `questions_has_sections` ON `questions`.`id` = `questions_has_sections`.`question_id` WHERE `questions_has_sections`.`section_id` = 1
ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'questions_has_sections': SELECT `questions`.* FROM `questions` INNER JOIN `questions_has_sections` ON `questions_has_sections`.`question_id` = `questions`.`id` INNER JOIN `questions_has_sections` ON `questions`.`id` = `questions_has_sections`.`question_id` WHERE `questions_has_sections`.`section_id` = 1
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/mysql2_adapter.rb:657:in `query'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/mysql2_adapter.rb:657:in `block in exec_query'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/abstract_adapter.rb:222:in `block in log'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activesupport-3.1.0.beta1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/abstract_adapter.rb:217:in `log'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/mysql2_adapter.rb:655:in `exec_query'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/mysql2_adapter.rb:649:in `select'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:12:in `select_all'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/base.rb:469:in `find_by_sql'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/relation.rb:106:in `to_a'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/relation/finder_methods.rb:155:in `all'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/associations/has_many_through_association.rb:137:in `find_target'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/associations/collection_association.rb:327:in `load_target'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/activerecord-3.1.0.beta1/lib/active_record/associations/collection_proxy.rb:73:in `method_missing'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/wirble-0.1.3/lib/wirble.rb:432:in `output_value'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/railties-3.1.0.beta1/lib/rails/commands/console.rb:44:in `start'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/railties-3.1.0.beta1/lib/rails/commands/console.rb:8:in `start'
from /Users/fonsan/.rvm/gems/ruby-1.9.2-p180@dok/gems/railties-3.1.0.beta1/lib/rails/commands.rb:39:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'dok(dev)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment