【问题标题】:Join array and hash加入数组和哈希
【发布时间】:2014-07-10 13:38:07
【问题描述】:

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

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

【问题讨论】:

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


【解决方案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">]

【讨论】:

  • 我明白了,TypeError:没有将哈希隐式转换为数组
  • 在哪一行? @all_answers[q.id] 的值是多少?
  • +1 没关系,错误是我的拼写错误。我收到了这个错误,Prawn::Errors::UnrecognizedTableContent 但它在我的另一个类中。我正在努力。
  • 效果很好!数组整齐地排列着问题,然后是所有答案。
  • 我认为他应该做一个ActiveRecord#joins,这是使用 ruby​​ Enumerables 解决这个问题的一种更简洁的方法
【解决方案2】:

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

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

【讨论】:

  • 我认为这不会让一个问题属于许多测试。我有一个连接表 question_tests 存储测试的问题 ID。答案属于一个问题。
猜你喜欢
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 2012-01-07
相关资源
最近更新 更多