Activerecord объединить с has_many: через - PullRequest
0 голосов
/ 29 июня 2010
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 должен включать 'schedules'. 'Shop_id' = 'shops'. 'Id' вместо 'schedules'. 'Id' = 'shops'. 'Shop_id'.Что я могу изменить в своих моделях, чтобы это произошло?

Вот модели для этих трех классов:

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: Schedule.find: all,: joins => [: shop,: area]

В 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

1 Ответ

0 голосов
/ 29 июня 2010

Я думаю, ваш запрос должен быть указан с помощью опции: include, например,

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

Параметр: join предназначен для указания логики объединения, такой как "INNER JOIN ..."

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...