具有2个父级别的activerecord模型

时间:2017-03-31 19:37:38

标签: 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为null。我希望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 AND 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的答案

由于

1 个答案:

答案 0 :(得分:0)

我认为您应该belongs_to使用:user:question(而不是has_one),并将用户手动设置为来自控制器中current_user的答案。