Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Rails调查样式应用程序-显示选项上的所有答案_Mysql_Ruby On Rails_Ruby_Voting_Vote - Fatal编程技术网

Mysql Rails调查样式应用程序-显示选项上的所有答案

Mysql Rails调查样式应用程序-显示选项上的所有答案,mysql,ruby-on-rails,ruby,voting,vote,Mysql,Ruby On Rails,Ruby,Voting,Vote,我是RubyonRails的新手,正在开发我的第一个深入应用程序。它有四个表:问题、选项、答案和用户。这里有一个问题列表,用户可以投票选择一个独特的选项(存储在答案联接表中),我正试图在表格关联中找到答案 以下是我设置个人RB文件的方式: class Question < ActiveRecord::Base has_many :options has_many :answers, :through => :options end class Option &

我是RubyonRails的新手,正在开发我的第一个深入应用程序。它有四个表:问题选项答案用户。这里有一个问题列表,用户可以投票选择一个独特的选项(存储在答案联接表中),我正试图在表格关联中找到答案

以下是我设置个人RB文件的方式:

class Question < ActiveRecord::Base
     has_many :options
     has_many :answers, :through => :options
end

class Option < ActiveRecord::Base
    belongs_to :question
    has_many :answers
end

class Answer < ActiveRecord::Base
    belongs_to :user
    belongs_to :question
    belongs_to :option
end

class User < ActiveRecord::Base
    has_many :answers
    has_many :questions, :through => :answers 
end
以及my index.html.erb文件中的表体:

<tbody>
   <% @questions.each do |question| %>
      <tr class="<%= cycle('lineOdd', 'lineEven') %>">
         <td><%= question.question_text %></td>
         <td><%= link_to 'Show', question %></td>
         <td><%= link_to 'Edit', edit_question_path(question) %></td>
         <td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
      <% question.options.each do |option_text| %>
         <tr class="backgroundColor1">
            <td class="optionCell"> <%= option_text.option_text %> </td>
         </tr>
      <% end %>
   <% end %>
</tbody>

在我使用的问题类中,“has_many:answers,:to=>:options”-这是正确的方法吗?我如何在相关选项下方的表格行中输出总投票数

我是否需要添加或更改问题控制器代码

这是我的第一篇文章,如果我没有提供足够的信息,很抱歉


谢谢

让我们先解决一下关系:

class Question < ActiveRecord::Base
  has_many :options
  has_many :answers
  has_many :users, through: :answers
end
这将创建一个
n+1
查询来获取每个选项的答案计数。所以我们要做的是在选项表上存储一个计数

让我们从创建迁移来添加列开始:

rails g migration AddAnswerCounterCacheToOptions answers_count:integer
rake db:migrate
然后我们告诉ActiveRecord在创建相关记录时更新计数,这看起来有点奇怪,因为
计数器缓存:true
声明位于
所属的
一侧,而列位于另一侧,但AR就是这样工作的

class Option < ActiveRecord::Base
  belongs_to :question
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :user
  belongs_to :question
  belongs_to :option, counter_cache: true
end
这可能需要一些时间,因为它需要提取每个选项并更新计数

现在我们可以这样显示计数:

<% question.options.each do |option| %>
  <tr class="backgroundColor1">
    <td class="optionCell"><%= option.option_text %></td>
    <td class="optionCell"><%= option.answers.size %></td>
  </tr>
<% end %>


.size
足够聪明,可以使用我们的计数器缓存列,但会返回到查询计数,这对于测试来说是一件好事。

替代
计数器缓存的方法是编写一个select子句,在子查询中获取计数。不建议初学者使用,因为您需要了解ActiveRecord如何构建查询和加载关系。非常感谢,非常有用!:-)看到第一个问题非常令人耳目一新,它虽然很好,但包含了所有相关信息。干得好。啊,谢谢-我现在真的很喜欢Rails,所以我希望能留下来!
class Option < ActiveRecord::Base
  belongs_to :question
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :user
  belongs_to :question
  belongs_to :option, counter_cache: true
end
Option.find_each { |option| Option.reset_counters(option.id, :answers) }
<% question.options.each do |option| %>
  <tr class="backgroundColor1">
    <td class="optionCell"><%= option.option_text %></td>
    <td class="optionCell"><%= option.answers.size %></td>
  </tr>
<% end %>