Rails Squeel 的 joins 怎麼用起來那麼不順?

idarfan · 2012年04月18日 · 最后由 idarfan 回复于 2012年04月19日 · 3100 次阅读

各位大俠大家好:

為了方便讓大家明白我的問題,也怕我水平不足無法正確陳述問題。我附上一些圖片 但不知會不會被和蟹掉。如果看不到圖請不吝告之那邊有可以正常運作的圖片網站我再去註冊。

Student.joins(:student_incomelevelships => {:incomelevels => :student}) 



我想把它們 joins 一起再由 view 那兒做出選單大概類似 squeel 範例裏所述的

Person.joins(:articles => {:comments => :person}) => SELECT "people".* FROM people" INNER JOIN "articles" ON "articles"."person_id" = "people"."id" INNER 
JOIN "comments" ON "comments"."article_id" = "articles"."id" INNER JOIN "people" people_comments" ON "people_comments"."id" = "comments"."person_id"



當我用這個指令時

Student.joins(:student_incomelevelships => {:incomelevels => :student}) 



卻一直出現底下的錯誤訊息

ruby-1.9.2-p290 :018 > Student.joins(:student_incomelevelships => {:incomelevels => :student}) ActiveRecord::ConfigurationError: Association named 'student_incomelevelships' was not found; perhaps you misspelled it? 
from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.7/lib/active_record /associations.rb:1944:in build' from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/polyamorous-0.5.0/lib/polyamorous/join_dependency.rb:52:inbuild_with_polymorphism' from /home/kojendirect/.rvm/gems/ruby-1.9.2-p290/gems/squeel-0.9.5/lib/squeel/adapters/active_record/join_dependency_extensions.rb:29:in `build_with_squeel'



請問這是什麼原因造成的?還是我指令有錯? 這是我Studen 資料表的截圖

這是中介資料表的截圖

這是收入等級資料表的截圖

你的 Student 和 StudentIncomelevelship 缺少关联关系(Association) 在 model 里声明 has_many 等,应该就可以了

joins 是 active_record 的吧?和 squeel 没啥关系吧?

vkill 您好。Squeel 有加強並簡化 joins 的應用 詳情請參閱 http://rubydoc.info/gems/squeel/file/README.md 部份原文提供如下:

Joins

Squeel adds a couple of enhancements to joins. First, keypaths can be used as shorthand for nested association joins. Second, you can specify join types (inner and outer), and a class in the case of a polymorphic belongs_to relationship.

Person.joins{articles.outer} => SELECT "people".* FROM "people" LEFT OUTER JOIN "articles" ON "articles"."person_id" = "people"."id" Note.joins{notable(Person).outer} => SELECT "notes".* FROM "notes" LEFT OUTER JOIN "people" ON "people"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Person'

These can also be used inside keypaths:

Note.joins{notable(Person).articles} => SELECT "notes".* FROM "notes" INNER JOIN "people" ON "people"."id" = "notes"."notable_id" AND "notes"."notable_type" = 'Person' INNER JOIN "articles" ON "articles"."person_id" = "people"."id"

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%'))

Keypaths were used here for clarity, but nested hashes would work just as well.

需要 登录 后方可回复, 如果你还没有账号请 注册新账号