我的模型设计有点复杂,它们之间有很多关联。 这是模型设计
用户模型
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
来源模型
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 模型
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::错误:“where 子句”中的未知列“users.user_id”:SELECT users
。* FROM users
INNER JOIN records
ON 记录
.user_id
= users
.id
WHERE users
. user_id
= 1 ActiveRecord::StatementInvalid:Mysql::Error:“where 子句”中的未知列“users.user_id”:SELECT users
。* FROM users
INNER JOIN records
ON records
.user_id
= users
.id
WHERE 用户
。user_id
= 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"
关于mysql - rails 中的关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24303551/