铁路协会无法正常工作

时间:2014-06-19 09:44:27

标签: mysql ruby-on-rails activerecord join ruby-on-rails-4

我有点复杂的模型设计,它们之间有许多关联。 这是模型设计

用户模型

class User < ActiveRecord::Base
has_many :records
has_many :sources
has_many :record_type_student
has_many :record_type_employee
has_many :record_type_other

validates :email, presence: true

end

Souces Model

class Source < ActiveRecord::Base

belongs_to :user

 has_many :records
 has_many :record_type_student
 has_many :record_type_employee
 has_many :record_type_other

end

记录模型

class Record < ActiveRecord::Base
 belongs_to :user
 belongs_to :source

 has_many :record_type_student
 has_many :record_type_employee
 has_many :record_type_other

end

RecordTypeStudent Model

  class RecordTypeStudent < ActiveRecord::Base
   belongs_to :record
 belongs_to :user
belongs_to :source

end

其他两个RecordTypeOther和RecordTypeEmployee的类似模型

我可以正确保存数据,并检查每个recordtype*表中包含record_id,现在我尝试使用includes访问数据。这是我想要查询的内容

 @records = Record.includes(:record_type_stduents, :record_type_others, :record_type_employees).where(User.find_by_email(params[:user].to_s).id).all;

但是我在加入

后只得到记录字段而不是数据

这是生成的查询

 Record Load (1.9ms)  SELECT `records`.* FROM `records` WHERE `records`.`user_id` =  1    RecordTypeStudent Load (7.9ms)  SELECT `record_type_students`.* FROM  `record_type_students` WHERE `record_type_students`.`record_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)  RecordTypeOther Load (2.4ms)  SELECT `record_type_others`.* FROM `record_type_students`    WHERE `record_type_students`.`record_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)  RecordTypeEmployee Load (2.0ms)  SELECT `record_type_employees`.* FROM `record_type_employees` WHERE `record_type_employees`.`recordt_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)

这是我尝试从用户模型中提取

的时候
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from irb_binding at (irb):15)

User Load (3.4ms)  SELECT `users`.* FROM `users` INNER JOIN `records` ON `records`.`user_id` = `users`.`id` WHERE `users`.`user_id` = 1

Mysql ::错误:未知列&#39; users.user_id&#39;在&#39; where子句&#39;:SELECT users。* FROM users INNER JOIN records ON recordsuser_id = usersid WHERE usersuser_id = 1 ActiveRecord :: StatementInvalid:Mysql ::错误:未知列&#39; users.user_id&#39;在&#39; where子句&#39;:SELECT users。* FROM users INNER JOIN records ON recordsuser_id = usersid WHERE usersuser_id = 1

1 个答案:

答案 0 :(得分:1)

我认为你最好使用STI (single table inheritance)

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :students, class: "User::Student"
   has_many :employees, class: "User::Employee"
   has_many :other, class: "User::Other"
end

#app/models/users/student.rb
Class Student < Record
end

#app/models/record.rb
Class Record < ActiveRecord::Base
   belongs_to :user
end

这将为您提供以下字段:

#users
id | user | attributes | created_at | updated_at

#records
id | type | user_id | other | record | attributes | created_at | updated_at

这将允许您致电:

user = User.find params[:id]
user.students # -> pulls records with type == "Student"