向javascript提供模型及其关系模型

向javascript提供模型及其关系模型,javascript,ruby-on-rails-4,Javascript,Ruby On Rails 4,我想提供一个性能模型,并将附加到它的模型(执行者模型)作为json链接到javascript 我知道我可以使用以下代码提供性能模型 <%= javascript_tag do %> window.performances = <%=raw Performance.order('date DESC').limit(99).to_json %>; <% end %> window.performances=; 现在,对于每个性能,我希望在同一个jso

我想提供一个性能模型,并将附加到它的模型(执行者模型)作为json链接到javascript

我知道我可以使用以下代码提供性能模型

<%= javascript_tag do %>
    window.performances = <%=raw Performance.order('date DESC').limit(99).to_json %>;
<% end %>

window.performances=;
现在,对于每个性能,我希望在同一个json文件中附加链接到它的执行者

我怎样才能做到这一点


为了澄清这一点,表演与许多表演者有着密切的关系。因此,在ruby文件中,我可以通过Performance访问它。performers

我还没有弄清楚如何自己获取执行者,但我使用了性能上的includes方法,在一个表中获取性能和执行者:

<%= javascript_tag do %>
window.performances = <%=raw Performance.includes(:performers).order('date DESC').to_json(:include => {:performers =>  {
                                                                                    :except => [ :created_at, :updated_at ]
                                                                                    }}, :except => [ :created_at, :updated_at ]) %> ;
<% end %>
如果有人知道一个更好的方法,让演员不表演,请张贴在下面,我接受它作为最佳答案

#you can use a loop to loop through each performance
currentPerformance = window.performances[fileIndex];
performers += "_" + currentPerformance.performers[0].name; 
#this will return the first performer name from performances at fileIndex