2014-05-21 75 views
0

Ruby on Rails 4加入数组和哈希

我想加入一个问题数组和哈希组合在一起的问题的ID。

哈希按来自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>]> 

我是新来的许多轨道/ Ruby方法。

+0

可能重复[使用Prawn使用属于collectionproxy的值的数组创建PDF](http://stackoverflow.com/questions/23768731/create-pdf-with-arr属于收集的代理使用虾) –

回答

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">] 
+0

我得到,TypeError:没有隐式转换哈希到数组 – DDDD

+0

在哪一行? '@all_answers [q.id]'的价值是什么? –

+0

+1没关系,错误是我的拼写错误。虽然我得到了这个错误,Prawn :: Errors :: UnrecognizedTableContent,但它在我的其他类中。我在做这个工作。 – DDDD

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 
+0

我不认为这将允许一个问题属于许多测试。我有一个连接表,用于存储测试的问题ID的question_tests。答案属于一个问题。 – DDDD