从Active Record结果中创建新哈希

时间:2016-04-08 07:53:16

标签: ruby-on-rails ruby-on-rails-4

我需要根据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问题时,它会给出这个

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

在我看来我有这个

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

<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)

我想向当前用户展示的是他没有触及的问题,因为他还没有关系。

我认为我的新结构有问题,因为我看到它不是一个数组?

3 个答案:

答案 0 :(得分:2)

尝试:

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

然后在你的视图中使用@all_questions,就像你写的一样。

答案 1 :(得分: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是一个Hash,当使用它时,它将键值对作为块参数传递,你需要两个参数,如果你只提供一个参数,它将是一个像[key,value]这样的数组]

答案 2 :(得分: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 %>