So I'm trying to combine two tables and show the results in order of the start_date. I've tried a few things but because its technically a nested loop its giving me double results for each item. The code i currently have is as follows
所以我试图组合两个表并按start_date的顺序显示结果。我尝试了一些东西,但因为它在技术上是一个嵌套循环,它给了我每个项目的双重结果。我目前的代码如下
<% @subcategory = Subcategory.all %>
<% @product = Product.all %>
<% (@product + @subcategory).each do |product, subcategory|%>
<% if product.display_on_home_page and !product.is_highlight_product and !(product == '..') or subcategory.
display_on_home_page and !subcategory.is_highlight_product and !(subcategory == '..')%>
<div class="column_entry">
<%= link_to image_tag(subcategory.image_attachment.url(:normal_page_size)), subcategories_content_url(subcategory.id), :controller=>'subcategories' %>
</div>
<% end %>
<% if product.price_from %>
<div class="column_entry">
<div class="product_special">
<span class="a">From Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% else %>
<div class="column_entry">
<div class="product_special">
<span class="a">Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% end %>
<% end %>
I know this is quite a long an complex statement, its supposed to loop through all of the subcategories and all of the products and display the images, there are also two different ways of displaying the price based on a boolean that says whether the price is a specific amount or it starts from a given price.
我知道这是一个相当长的复杂陈述,它应该循环遍历所有子类别和所有产品并显示图像,还有两种不同的方式来显示价格基于一个布尔值,表明价格是否是特定金额或从特定价格开始。
at the moment its reading through the loop but its giving me the error
目前它通过循环阅读,但它给了我错误
undefined method `is_highlight_product' for nil:NilClass
since this is the first column in the table that is referenced and its breaking here I think that there must be some conflict in its ability to see the information stored in the table.
因为这是引用的表中的第一列并且它在这里打破了我认为它在查看存储在表中的信息的能力中肯定存在一些冲突。
I'm still quite new to ruby on rails so any help or even just a nudge in the right direction would be very much appreciated.
我仍然对铁轨上的红宝石很新,所以任何帮助,甚至只是在正确的方向轻推都会非常感激。
If you would like more information just ask in the comments and I'll put it up as fast as I can.
如果您想了解更多信息,请在评论中提出,我会尽快将其提出来。
1 个解决方案
#1
1
The problem here is, when you do something like this:
这里的问题是,当你做这样的事情时:
(@product + @subcategory).each do |product, subcategory|
The local variable product
will iterate firstly through products, then through subcategories, and the local variable subcategory
will always be nil.
局部变量产品将首先通过产品迭代,然后通过子类别迭代,局部变量子类别将始终为零。
What you can do, a dirty way - check
你可以做什么,一个肮脏的方式 - 检查
if product.is_a?(Product)
# do your things
elsif product.is_a?(Subcategory)
# do other things
end
#1
1
The problem here is, when you do something like this:
这里的问题是,当你做这样的事情时:
(@product + @subcategory).each do |product, subcategory|
The local variable product
will iterate firstly through products, then through subcategories, and the local variable subcategory
will always be nil.
局部变量产品将首先通过产品迭代,然后通过子类别迭代,局部变量子类别将始终为零。
What you can do, a dirty way - check
你可以做什么,一个肮脏的方式 - 检查
if product.is_a?(Product)
# do your things
elsif product.is_a?(Subcategory)
# do other things
end