gpt4 book ai didi

ruby-on-rails - 如何在 ActiveRecord 中建模问答数据结构?

转载 作者:行者123 更新时间:2023-12-04 03:35:02 25 4
gpt4 key购买 nike

这感觉应该非常简单,但我一直没能把它做好。

在我的应用程序中,我想要问题答案

一个Question只能有1个Answer,但是一个Answer可以用于多个Question

例如。

问题表数据

  1. “二加二等于?”
  2. “正方形的边数?”

答题表数据

  1. 四个

两个问题只有1个答案,但是答案记录可以用于两个问题

我想这也许行得通::

rails g resource question verbiage:string answer:references

但是我必须在 Question 模型上放置一个 belongs_to :answer ,这似乎不对。

感觉应该可以做类似::

Question.first.answer # returns the single answer

Answer.first.questions # returns all of the Questions where this record is the Answer

谁能教我在 ActiveRecord 中建模的正确方法?

最佳答案

你需要has_many协会行动。我要为此使用脚手架。

  1. 创建答案:

    rails g scaffold Answer value:string
  2. 创建问题:

    rails g scaffold Question verbiage:string answer:references
  3. 运行 rails db:migrate

  4. 创建关联。

    class Answer < ApplicationRecord
    has_many :questions
    end

    class Question < ApplicationRecord
    belongs_to :answer
    end

关于ruby-on-rails - 如何在 ActiveRecord 中建模问答数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com