各位大俠大家好: 關於 Squeel 0.95.0 的 joins 真是越參越不明白。。底下是官方的示例 You can refer to these associations when constructing other parts of your query, and they'll be automatically mapped to the proper table or table alias This is most noticeable when using self-referential associations: 構建您的查詢的其他部分時,你可以參考這些關聯,他們將被自動映射到正確的表或表別名,當使用自我參照關聯這是最引人注目的:
Person.joins{children.parent.children}.
where{
(children.name.like 'Ernie%') |
(children.parent.name.like 'Ernie%') |
(children.parent.children.name.like 'Ernie%')
}
=> SELECT "people".* FROM "people"
INNER JOIN "people" "children_people" ON "children_people"."parent_id" = "people"."id"
INNER JOIN "people" "parents_people" ON "parents_people"."id" = "children_people"."parent_id"
INNER JOIN "people" "children_people_2" ON "children_people_2"."parent_id" = "parents_people"."id"
WHERE ((("children_people"."name" LIKE 'Ernie%'
OR "parents_people"."name" LIKE 'Ernie%')
OR "children_people_2"."name" LIKE 'Ernie%'))
這段是我照著拿來改改看能不能用的。怎麼改也死活跑不出來 真令人納悶 有誰能幫我睇下這個惱人的問題 感謝
ruby-1.9.2-p290 :040 > Student.joins{StudentIncomelevelship}.
ruby-1.9.2-p290 :041 > where{
ruby-1.9.2-p290 :042 > (StudentIncomelevelship.incomelevel_id == 1)
ruby-1.9.2-p290 :043?> }
ruby-1.9.2-p290 :044?> }
NoMethodError: undefined method `incomelevel_id' for #<Class:0x0000000308b620>
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.7/lib/active_record/base.rb:1009:in `method_missing'
from (irb):42:in `block (2 levels) in irb_binding'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/dsl.rb:31:in `instance_eval'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/dsl.rb:31:in `eval'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/adapters/active_record/3.0/relation_extensions.rb:219:in `where'
from (irb):40:in `block in irb_binding'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/dsl.rb:31:in `instance_eval'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/dsl.rb:31:in `eval'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/adapters/active_record/3.0/relation_extensions.rb:219:in `where'
from (irb):36
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'