Rails 一对多关系,结果为 null

发布于 2025-01-04 09:49:04 字数 817 浏览 2 评论 0原文

我有一个简单的数据库,具有以下关系

每个问题都有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2025-01-11 09:49:04

问题的类型是问题的属性,而不是相反。因此,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 should has_one :questiontype and the Questiontype should belongs_to :question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文