加入数组和哈希

时间:2014-05-21 18:34:05

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

Ruby on Rails 4

我正在尝试加入一系列问题,并按问题的ID一起按哈希分组。

Hash按其Answers记录的question_id属性进行分组。

数组包含所有问题,索引为id。

我试图将它们放入一个数组中,@ pdf。问题首先是枚举器索引的答案。但它表现得很奇怪。

@pdf = []
@test = Test.find(params[:id])
@test_questions = @test.questions
answers = Answer.find(:all)
@all_answers = answers.group_by(&:question_id)
@test_questions.each do |q|
  @pdf << q
  @pdf << @all_answers.select { |i| i == q }
end

日志显示:

=> [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, {}, #<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>, {}]

这是@all_answers:

=> {1=>[#<Answer id: 1, content: "It walked", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 2, content: "It was thrown", question_id: 1, correct: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 3, content: "It got run over and pushed", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">], 2=>[#<Answer id: 4, content: "False", question_id: 2, correct: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">, #<Answer id: 5, content: "True", question_id: 2, correct: false, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">]}

这是@test_questions:

=> #<ActiveRecord::Associations::CollectionProxy [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, #<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>]>

我是许多Rails / Ruby方法的新手。

2 个答案:

答案 0 :(得分:1)

我认为这是您需要的代码:

@pdf = []
@test = Test.find(params[:id])
@test_questions = @test.questions
answers = Answer.find(:all)
@all_answers = answers.group_by(&:question_id)
@test_questions.each do |q|
  @pdf << q
  @pdf += @all_answers[q.id]
end

这应该创建如下:

=> [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, #<Answer id: 1, content: "It walked", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 2, content: "It was thrown", question_id: 1, correct: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 3, content: "It got run over and pushed", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, 
    #<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>, #<Answer id: 4, content: "False", question_id: 2, correct: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">, #<Answer id: 5, content: "True", question_id: 2, correct: false, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">]

答案 1 :(得分:0)

你可能想要做的是这样的事情:

class Test < ActiveRecord::Base
  has_many :questions
  has_many :answers, :through => :questions
end

class Question < ActiveRecord::Base
  belongs_to :test
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

然后做:

test = Test.find(params[:id]).joins(:questions, :answers)

这将在一个查询中获取问题和答案,您可以执行以下操作:

test.questions.first.answers

test.answers

和pdf:

test.questions.each do |q|
pdf = test.questions.collect do |q|
  [q, q.answers]
end