ruby-on-rails - 具有 2 个父级别的事件记录模型

标签 ruby-on-rails ruby rails-activerecord ruby-on-rails-5

一个用户可以有很多问题。许多用户可以参与一个问题并对同一个问题提供多个答案。用户和问题之间的关系似乎工作正常。问题在于答案。

模型/user.rb

  has_and_belongs_to_many :questions
  has_many :answers

模型/question.rb

  has_and_belongs_to_many :users
  has_many :answers

模型/answer.rb

  has_one :question
  has_one :user, :through => :question

这是我在控制台中尝试的内容:

irb(main):022:0> u1.questions.first.answers.create(answer: "foo1", order:1)
   (0.2ms)  BEGIN
  Answer Exists (0.5ms)  SELECT  1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2  [["answer", "foo1"], ["LIMIT", 1]]
  SQL (0.4ms)  INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "foo1"], ["order", 1], ["question_id", 1]]
   (13.5ms)  COMMIT
=> #<Answer id: 6, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "foo1", order: 1, user_id: nil, question_id: 1>
irb(main):023:0> u1.questions.first.answers.create(answer: "bar1", order:2)
   (0.4ms)  BEGIN
  Answer Exists (0.9ms)  SELECT  1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2  [["answer", "bar1"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "bar1"], ["order", 2], ["question_id", 1]]
   (19.9ms)  COMMIT
=> #<Answer id: 7, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "bar1", order: 2, user_id: nil, question_id: 1>
irb(main):024:0> u2.questions.first.answers.create(answer: "foo2", order:1)
   (0.7ms)  BEGIN
  Answer Exists (0.6ms)  SELECT  1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2  [["answer", "foo2"], ["LIMIT", 1]]
  SQL (0.9ms)  INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "foo2"], ["order", 1], ["question_id", 1]]
   (16.7ms)  COMMIT
=> #<Answer id: 8, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "foo2", order: 1, user_id: nil, question_id: 1>
irb(main):025:0> u2.questions.first.answers.create(answer: "bar2", order:2)
   (0.5ms)  BEGIN
  Answer Exists (0.6ms)  SELECT  1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2  [["answer", "bar2"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", 2017-03-31 19:31:05 UTC], ["updated_at", 2017-03-31 19:31:05 UTC], ["answer", "bar2"], ["order", 2], ["question_id", 1]]
   (14.9ms)  COMMIT
=> #<Answer id: 9, created_at: "2017-03-31 19:31:05", updated_at: "2017-03-31 19:31:05", answer: "bar2", order: 2, user_id: nil, question_id: 1>

鉴于 user_id 为空这一事实,很明显出了点问题,除非我指定。我希望填充 user_id,因为我是通过问题从用户对象创建它的。

irb(main):027:0* u1.questions.first.answers.count
   (0.7ms)  SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1  [["question_id", 1]]
=> 4

正如我在这里看到的,它只是按 question_id 进行过滤。我希望从 question_id 和 user_id 中检索答案

这个问题能解决吗?我应该换一种方式吗?

谢谢

更新

总而言之,这是我想要实现的目标:

User.first.answers.where(question_id: 1)

查询看起来像:

SELECT "answers".* FROM "answers" WHERE "answers"."user_id" = $1 AND "answers"."question_id" = $2  [["user_id", 1], ["question_id", 1]]

我希望能够做到:

User.first.questions.first.answers

并获取 user_id 和 question_id 的答案

谢谢

最佳答案

我认为你应该为 :user:question 使用 belongs_to(而不是 has_one)并设置用户从 Controller 中的 current_user 手动获取您的答案。

关于ruby-on-rails - 具有 2 个父级别的事件记录模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43148442/

相关文章:

ruby-on-rails - 如何在 Ruby 中创建一个新的 Date 实例

ruby-on-rails - Money-Rails gem : Humanized_money in Models

ruby-on-rails - 为什么 `config.force_ssl = true` 不是生产 rails 应用程序的默认值?

ruby-on-rails - 默认情况下的 Minitest-Rails 规范样式测试

ruby-on-rails - 如何将递归/嵌套的 OpenStruct 对象转换为哈希

python - 交互式 CLI 包 - 复选框和选择

ruby - 在 Ruby 中使用 GTK3 自定义信号

ruby-on-rails - 有没有在 Arel NamedFunction 节点中使用 ORDER BY 的干净方法?

ruby-on-rails - 为 Rails 中的现有 Postgres View 获取 "Relation does not exists"

ruby-on-rails - rails : Active Model explanation?