我试图弯曲传统数据库周围的轨道。每个表都有一个'数据源'列,它是数据来源的3个字符的代码。因此,为了唯一标识和关联其他表格,您必须拥有两条信息(例如帐号和数据源)。Rails has_many与多个键的关联
我有下面的has_many关系很好,除非我试图包含在查询中。
# Model Association
has_many :transactions,
:primary_key => :account,
:foreign_key => :acnt,
:order => :ddate,
:conditions => ['datasource = ?', '#{self.datasource}']
# Controller code FAIL when I loop through results in view and call 'account.transactions'
@accounts = Account.includes(:transactions).where(:lname => 'Smith')
# However, this controller code works when I loop through the results in the view and call 'account.transactions'
@accounts = Account.where(:lname => 'Smith')
# View
<% @accounts.each do |a| %>
<% a.transactions.each do |t| %>
<%= t.description %>
<% end %>
<% end %>
错误:
ActionView::Template::Error (undefined method `datasource' for #<Class:0x1025f25c8>):
什么是Rails 3中做到这一点的正确方法?
显示你的错误 – fl00r 2011-03-13 17:03:19