2016-04-08 8 views
0

私は自分のデータベースに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) 

は、彼のように、彼は触れていない質問ですまだ関係はありません。

私はそれが配列になっていないと私は私の新しい構造に何か間違っていると思いますか?

+1

はあなたが達成したい正確に何書きますか?意味、あなたがビューに表示したいもの – Aleks

+0

@Aleksちょっと私はいくつかの情報を追加しました。 –

+0

回答を投稿しましたが、あなたの質問テーブルにユーザの列が表示されません。それはまったく存在しますか?ユーザーと質問をリンクさせるにはどうすればよいですか? – Aleks

答えて

2

はして試してみてください。

@all_questions = Question.where.not(id: current_user.question_ids) 

そして、あなたが書いただけのようなあなたのビューで@all_questionsを使用しています。

+0

ねえ、クリーナーで働いていても!しかし、私はこれに関連する別の問題がある、あなたはここで確認できますか? 'http:// stackoverflow.com/questions/36496370/pagination-not-a-certain-query-only-a-certain-query''キーを使用しない場合は –

2

変更

<% @all_questions.each do |q| %> 
    <p class="question-title"> <%= q.description %> </p> 
<% end %> 

へ:

<% @all_questions.each do |_, q| %> 
    <p class="question-title"> <%= q.description %> </p> 
<% end %> 

あなたが本当にキーを必要としない場合は、よりよい解決策は次のようになります。

<% @all_questions.each_value do |q| %> 
    <p class="question-title"> <%= q.description %> </p> 
<% end %> 

@all_questions、ハッシュですそれらをそれぞれ使用するときに、キー値ペアをブロックパラメーターとして渡すと、1つのパラメーターだけを提供すれば、それらのパラメーターが2つ必要ですameterの場合、[key、value]のような配列になります。お使いのコントローラで

+2

'@all_questions.each do | _、q | ' '。 – born4new

+0

@ born4new良い点! –

+0

これは素晴らしい作品です!ありがとうございます。 –

1

:ビューで

@all_questions = Question.includes(:users).where("questions.id NOT IN (?)", current_user.questions.pluck(:id)) 

<% @all_questions.each do |q| %> 
<p class="question-title"> <%= q.description %> </p> 
<% end %> 
+0

@PetrosKyriakou解決策があることは分かっています。私は現在のユーザーに関連していないすべての質問を取得し、あなたの意見に表示しようとしました。これが解決策に適合するかどうか私にお知らせください。 – dp7

+0

これを今試しました!これはコードが少なくてすみ、最善の解決策です。私はこれを解決策としてマークします! –

+0

解決策は機能しますが、さらに多くの操作が必要です。 – Aleks

関連する問題