【发布时间】:2011-07-14 12:52:47
【问题描述】:
我正在尝试绕过旧数据库。每个表都有一个“数据源”列,该列是数据来源的 3 个字符代码。因此,为了唯一标识并与其他表相关联,您必须拥有两条信息(例如帐号和数据源)。
除非我尝试在查询中包含,否则我在下面的 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 中实现此目的的正确方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby activerecord associations models