私は自分のデータベースにcurrent_userに基づいている質問を抽出し、現在のユーザーに提示する必要があります。アクティブレコードの結果から新しいハッシュを作成する
私はデータベースにすべての質問がありますが、ユーザーと質問の関係上、current_userが存在しないものだけが必要です。
私のコードは、これまでのところ、この
@all_questions = Hash.new
counter = 0
ill_questions = Question.all
ill_questions.each do |q|
if !q.users.include? current_user
@all_questions[counter] = q
counter += 1
end
end
puts "these are all unanswered questions #{@all_questions}"
ようになり、私は@all質問を印刷するとき、それは私がこの
<% @all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
が上のエラーを取得してい私の見解では、この
{0=>#<Question id: 9, question_type: "multiple", description: "This is a question", expl
anation: "", category_id: 2, created_at: "2015-11-05 20:02:05", updated_at: "2016-02-11 19:23:02", link_name: "", link: "",
video_url: "", image: nil, image_position: "explanation", metric: "metric test", imperial: "testers", description_type: tr
ue, restriction: false>, 1=>#<Question id: 10, question_type: "single", description: "This is another question", explanatio
n: "test", category_id: 10, created_at: "2015-12-10 12:57:10", updated_at: "2016-01-12 23:36:25", link_name: "", link: "",
video_url: "", image: nil, image_position: "question", metric: nil, imperial: nil, description_type: true, restriction: tru
e>, 2=>#<Question id: 11, question_type: "single", description: "correct-wrong", explanation: "", category_id: 11, created_
at: "2016-01-29 19:53:48", updated_at: "2016-01-29 19:53:48", link_name: "", link: "", video_url: "", image: nil, image_pos
ition: "question", metric: nil, imperial: nil, description_type: true, restriction: true>, 3=>#<Question id: 12, question_t
ype: "single", description: "New question", explanation: "", category_id: 10, created_at: "2016-01-29 19:54:18", updated_at
: "2016-01-29 19:54:18", link_name: "", link: "", video_url: "", image: nil, image_position: "question", metric: nil, imper
ial: nil, description_type: true, restriction: true>}
を与えます<p class="question-title"> <%= q.description %> </p>
undefined method "description" for #<Array:0x007fe15ba18c88>
ユーザーと質問
User has_many :questions, :through => :trackers
Question has_many :users,through: :trackers
との関係は、私が現在のユーザーに表示したいと思い何トラッカーモデル
Tracker(id: integer, user_id: integer, question_id: integer, answered: boolean, correct: boolean, created_at: datetime,
updated_at: datetime, category_id: integer)
は、彼のように、彼は触れていない質問ですまだ関係はありません。
私はそれが配列になっていないと私は私の新しい構造に何か間違っていると思いますか?
はあなたが達成したい正確に何書きますか?意味、あなたがビューに表示したいもの – Aleks
@Aleksちょっと私はいくつかの情報を追加しました。 –
回答を投稿しましたが、あなたの質問テーブルにユーザの列が表示されません。それはまったく存在しますか?ユーザーと質問をリンクさせるにはどうすればよいですか? – Aleks