2017-03-31 74 views
2

用戶可以有很多問題。許多用戶可以參與一個問題,併爲同一問題提供多個答案。用戶和問題之間的關係似乎工作正常。問題與答案。有兩個父級別的activerecord模型

模型/ 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

這個問題能解決嗎?我應該用另一種方式來做嗎?

感謝

UPDATE

作爲總結,這是我想達到的目標:

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

感謝

+0

不應答屬於用戶。如果我要回答這個問題,答案將屬於我。 – engineersmnky

+0

是的,它需要下到用戶,但通過一個問題。我想要做的是user.questions.find(1).answers,這將返回此特定用戶完成的問題的答案。 – Seoman

回答

0

我認爲你應該在控制器使用belongs_to:user:question(而不是has_one),並設置用戶手動到你的答案來自current_user

+0

現在我有以下錯誤:MethodError:未定義的方法'問題'爲#<答案:0x00000002e7c280>在原始問題中使用命令創建新答案時。 – Seoman

相關問題