Rails 5命令(:name)有很多关联

时间:2018-05-02 15:03:17

标签: ruby-on-rails ruby-on-rails-5

我有两个模型,productvariation产品has_many变体和变体belongs_to product。我在模板中显示产品的变体,但似乎无法使order(:name)variation关联一起使用 - 下面的控制器代码想要订购产品名称。

products_controller.rb

...
@products = Product.order(:name)
...

product.html.erb

...
<% @products.each do |product| %>
  <div class="grid-filter">
   <% product.variations.each do |v| %>
     <span class="copy">
       <%= v.name %>
     </span>
   <% end %>
  </div>
<% end %>
...

3 个答案:

答案 0 :(得分:1)

如果要基于变体名称对产品进行排序,则可以尝试在产品模型中的关联本身上添加订单,如下所示:

class Product
 has_many :variations, -> {order 'name'}
end

答案 1 :(得分:0)

您可以在视图中使用订单作为&#39;变体&#39;也是协会

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, 
                   initial-scale=1.0">
    
    <link rel="stylesheet" href="test.css">
    <script src="myScript.js"></script>
    
    <title>Luka Novak</title>
</head>

<body>
<div class="page-wrap">
        
    <div class="header">
        <div class="logo">
            
        </div>
        <div class="networks">
            <a href="#"><img src="facebook-symbol.svg" class="socialnet" alt="facebook"></a>
            <a href="#"><img src="instagram-symbol.svg" class="socialnet" alt="instagram"></a>
        </div>
            <div class="menuIcon">
            <button onclick="myFunction()" class="navButton">
                <img src="https://cdn3.iconfinder.com/data/icons/gray-toolbar/512/menu-512.png" 
                     alt="menu" 
                     class="iconImg">
            </button>
            </div>
    </div>
    
    <div class="sidenav col-5" id="menu">
        <a href="#" class="navlink">about us</a>
        <a href="#" class="navlink">services</a>
        <a href="#" class="navlink">contact</a>
    </div>
    
    <div class="main col-18">
        
        <article class="textbox">
        Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
        </article>
        
        <div class="image-row">
            <div class="image1">
            
            </div>
                
            <div class="image2">
                    
            </div>
                
            <div class="image3">
                
            </div>
        </div>
    </div>
    
    
    
    <div class="footer col-24">
        <p class="foot1">Some info</p>
        <p class="foot2">design by me</p>
        <p class="foot3">More info</p>
    </div>
    
</div>
    
</body>    

</html>

答案 2 :(得分:0)

您可以在控制器中订购:

...
@products = Product.order(:name)
                   .includes(:variants)
                   .order('variants.name')
...