一对多关系 fom_for 从所有模型属性中取最后 3 个

One to many relationship fom_for take the last 3 from all the model attributes

我有一对多的关系(oferta 有很多签名)我想显示所有 oferta 的最后三个签名

我试过这段代码,但它显示了每个 oferta 的最后 3 个签名

在家index.erb

<% @oferta.each do |o| %>
  <% if o.sigs.exists? %>
    <% for item in o.sigs.order("created_at asc").last(3).each %>
      <div class="col-md-4 col-sm-4">
        <div class="coll">
          <br>
          <%= link_to item do %>
            <%= image_tag item.image.url(), skip_pipeline: true ,id: "img",height: "200px"%>
          <% end %>
          <h4><%=link_to item.name,item %></h4>
          <p id="comment"><%= item.comment %></p>
          <%= link_to "read more..", item %> 
          <p id="price"><%= item.price %></p>
        </div>
      </div>
    <% end %>
  <% end %>
<% end %>

在控制器中

def index
  @oferta = Ofertum.unscoped.first(3)
end

在公证模型中

has_many :sigs

在 sig 模型中

belongs_to :ofertum

两种方式都可以,

@last_3_sigs = Sigs.last(3)

首先使用这个

获取最新记录
@last_3_sigs = Sig.order(created_at: :desc).limit(3)