Rails 一对多关系,结果为 null
我有一个简单的数据库,具有以下关系
每个问题都有一个 Questiontype (3 种不同的类型,例如 Questiontype1 , Questiontype2 , Questiontype3 )
在 Questiontype 中有 Question_id
- Question.rb (模型)
班级问题< ActiveRecord::基础 属于:问题类型1 属于:问题类型2 属于:问题类型3 结束
- Questiontype1.rb(模型)
类 Questiontype1 < ActiveRecord::Base has_many :questions , :foreign_key => “question_id”结束
- show.json.rabl (我使用 Rabl)
对象@question属性:id子:questiontype1 => :questiontype1 做属性 :id end
但是,当我从 Questiontype1 获取子项时,结果为 null
我可以解决这个问题吗?
抱歉我的英语不好,谢谢。
I have a simple database that has the following relation
Each Question has one Questiontype (3 different typs , such as Questiontype1 , Questiontype2 , Questiontype3 )
in Questiontype has question_id
- Question.rb (Model)
class Question < ActiveRecord::Base
belongs_to :questiontype1
belongs_to :questiontype2
belongs_to :questiontype3
end
- Questiontype1.rb (Model)
class Questiontype1 < ActiveRecord::Base has_many :questions ,
:foreign_key => "question_id" end
- show.json.rabl (I used Rabl)
object @question attributes :id child :questiontype1 =>
:questiontype1 do attributes :id end
However, when I get child from Questiontype1 , result is null
Can I solve this problem?
Sorry for my English , Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题的类型是问题的属性,而不是相反。因此,
Question
应该has_one :questiontype
并且Questiontype
应该belongs_to :question
。The type of a question is an attribute of the question, not the other way around. So the
Question
shouldhas_one :questiontype
and theQuestiontype
shouldbelongs_to :question
.