现有协会
class Job < ActiveRecord::Base
belongs_to :client
belongs_to :contact
belongs_to :location
has_one :geofence, :through => :location
end
class Client < ActiveRecord::Base
has_many :contacts
has_many :locations
end
class Contact < ActiveRecord::Base
belongs_to :client
end
class Location < ActiveRecord::Base
belongs_to :client
belongs_to :geofence
end
class Geofence < ActiveRecord::Base
has_many :locations
end
在尝试从各种表中查找工作搜索字段时,我做了这样的事情
@jobs = Job.find(:all, :joins => [:client,:contact,:location,:geofence], :conditions => ['geofences.address like ?',"%#{params[:term]}%"])
这会产生以下错误
ActiveRecord::StatementInvalid (Mysql::Error: Unknown column 'geofences_jobs_join.location_id'
从以下查询
SELECT `jobs`.* FROM `jobs` INNER JOIN `clients` ON `clients`.id = `jobs`.client_id INNER JOIN `contacts` ON `contacts`.id = `jobs`.contact_id INNER JOIN `locations` ON `locations`.id = `jobs`.location_id INNER JOIN `locations` geofences_jobs_join ON (`jobs`.`id` = `geofences_jobs_join`.`location_id`) INNER JOIN `geofences` ON (`geofences`.`id` = `geofences_jobs_join`.`geofence_id`) WHERE ...
有没有办法通过作业模型中的不同关联或查询的连接部分来解决此问题?
我可以使用 find_by_sql 得到我需要的东西,但如果可能的话,我想避免这种情况。