0

I need to extract some of the questions i have in my database based on current_user and present them to the Current User.

So i have all questions in database, but i need only those that the current_user does not exist on the relationship between users and questions.

My code so far goes like this

    @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}"

when i print @all questions it gives this

{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>}

in my view i have this

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

getting error on <p class="question-title"> <%= q.description %> </p>

undefined method "description" for #<Array:0x007fe15ba18c88>

the relationship between User and Question

User has_many :questions, :through => :trackers
Question has_many :users,through: :trackers

Tracker model

Tracker(id: integer, user_id: integer, question_id: integer, answered: boolean, correct: boolean, created_at: datetime,
updated_at: datetime, category_id: integer)

What i would like to show to the current user is the questions that he did not touch, as in he does not have a relationship yet.

i assume i have something wrong in my new structure, as i see not it became an array??

Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82

3 Answers3

2

Try with:

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

And then use @all_questions in your view just like you wrote.

Aleks
  • 4,866
  • 3
  • 38
  • 69
  • hey even cleaner and working! but i have another problem related to this, can you check here? `http://stackoverflow.com/questions/36496370/pagination-not-working-only-with-a-certain-query` – Petros Kyriakou Apr 08 '16 at 09:50
2

Change

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

To:

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

If you really don't need the key, a better solution will be:

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

@all_questions is a Hash, when use each on it, it passing the key-value pair as block parameters, you need two parameters for them, if you only provide one parameter, it will be an array like [key, value].

lei liu
  • 2,735
  • 1
  • 16
  • 18
  • 2
    `@all_questions.each do |_, q|` if you don't use `key`. – born4new Apr 08 '16 at 08:12
  • thank you this works great!can you add a bit of explanation thought so that i can understand better? – Petros Kyriakou Apr 08 '16 at 08:14
  • thank you i needed the `each_value` really! But dkp added a more neat solution with less boilerplate for my controller. i ought to mark that solution as the answer as its a single line and propably might be faster than looping. – Petros Kyriakou Apr 08 '16 at 09:19
1

In your controller:

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

In views:

<% @all_questions.each do |q| %>
 <p class="question-title"> <%= q.description %> </p>
<% end %>
dp7
  • 6,651
  • 1
  • 18
  • 37
  • @PetrosKyriakou I know you have got a solution. I have tried to fetch all questions which are not related to current user and then show them in your views. Please let me know if this fits as a solution or not. – dp7 Apr 08 '16 at 09:06
  • just tried this now! this is the neatest solution as it requires less code. I will mark this as the solution! – Petros Kyriakou Apr 08 '16 at 09:22
  • The solution would work, but still uses much more operations then it should have. – Aleks Apr 08 '16 at 09:44