- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的模型设计有点复杂,它们之间有很多关联。这是模型设计
用户模型
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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!