2010-06-29 108 views
0
ActiveRecord::StatementInvalid: 
Mysql::Error: Unknown column 'schedules.id' in 'on clause': 
SELECT `schedules`.* FROM `schedules` INNER JOIN `shops` ON 
(`schedules`.`id` = `shops`.`shop_id`) INNER JOIN `areas` ON 
(`areas`.`id` = `shops`.`area_id`) 

正確的SQL語句應該包括'時間表 'shop_id'= '商店' '身份證',而不是 '時間表' '身份證'= '商店' 'shop_id' 。 我可以在我的模型中更改什麼來實現此目的?ActiveRecord的加盟與的has_many:。通過

下面是這三個類模型: Schedule.find:

class Area < ActiveRecord::Base 
    has_many :shops 
    has_many :schedules, :through => :shop 
end 

class Schedule < ActiveRecord::Base 
    belongs_to :shop 
    has_many :areas, :through => :shop 
end 

class Shop < ActiveRecord::Base 
    belongs_to :area # foreign key - area_id 
    has_many :schedule 
end 

是創建SQL命令的命令所有,:加入=> [:店,:區]

在DB/schema.rb我:

create_table "areas", :force => true do |t| 
    t.string "campus" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

create_table "shops", :force => true do |t| 
    t.integer "area_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

create_table "schedules", :force => true do |t| 
    t.integer "shop_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 
+0

這將有助於看到你的數據庫方案的db/schema.rb和產生的SQL代碼。 – thomasfedb 2010-06-29 14:32:22

+0

不確定easy join語法是否適用於:through。當事情變得棘手時,我只是使用詳細格式: :joins => [「INNER JOIN商店ON日程安排'。'shop_id'='shops'。'id'」,...] – aceofspades 2010-06-29 16:11:28

回答

0

我覺得你的查詢應該使用指定:包括像這樣

012選項
Schedule.find :all, :include => [:shops, :areas] 

的:連接選項是指定加入邏輯,諸如「INNER JOIN ...」

相關問題