我很难从两个独立的表中获取数据,以显示在同一个索引页上。
我有一个表中的数据显示为一个列表,这个表被称为" transactions"。
现在在我的交易模型中,我已经定义了它有很多"注释。这是我的另一个表,我想在同一个索引上显示数据。
我希望每个事务都显示与事务ID相关联的注释以及在transaction_id下的notes表中的id。
对于我的生活,我还没有弄清楚如何做到这一点。
以下是我的交易index.html.erb的样子:
<% @transactions.each do |transaction| %>
<div class="element">
<%= trash_ico(transaction) %>
<%= edit_ico(transaction, edit_transaction_path(transaction)) %>
<%= link_to "<h2><span class=\"el_header\">#{Asset.find(transaction.asset_id).nmc_name} : Assigned to #{User.find(transaction.user_id).name}</span></h2>
<div class=\"el\">Created: #{transaction.created_at}</div>
<div class=\"el\">Ended: #{transaction.finished}</div>
<div class=\"el\">Type: #{if ! transaction.transaction_type.nil? then transaction.transaction_type.name end}</div>
<div class=\"el\">Status: #{if ! transaction.transaction_status.nil? then transaction.transaction_status.name end}</div>
<br clear=\"all\" />".html_safe, transaction %>
<% note.each do |note| %>
<%= link_to "<div>#{note.notes}</div>".html_safe, note %>
<% end %>
</div>
<% end %>
这就是为索引定义控制器的方式:
def index
unless params[:asset_id].nil?
@transactions = Transaction.find_all_by_asset_id(params[:asset_id])
else
@transactions = Transaction.all
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @transactions }
end
end
我非常喜欢编程,但主要是使用rails。我看到我需要做的任务是什么,但我不确定这样做的语法是什么。
我的show def有类似的视图设置。我只是不能在索引上的各自交易下面显示笔记。
答案 0 :(得分:1)
我在想,就像这样;
<% transaction.notes.each do |note| %>
<%= link_to "<div>#{note.notes}</div>".html_safe, note %>
<% end %>
由于您声明属于交易的票据。 Rails允许您以这种方式访问它们。