我似乎无法弄清楚为什么这种关联不起作用:我不确定如何比提供代码示例更好地描述情况:
这是我的应用程序环境:
Ruby version 1.8.7 (universal-darwin10.0)
Rails version 2.3.3
代码示例:
class Account
has_one :subscription
has_one :package, :through => :subscription
end
class Subscription
belongs_to :account
belongs_to :package
end
class Package
has_many :subscriptions
has_many :accounts, :through => :subscriptions
end
>> Account.first.subscription
=> #<Subscription id: 1, account_id: 25, package_id: 4>
>> Account.first.subscription.package
=> #<Package id: 4, name: "Bronze Package">
>> Package.first.accounts
=> [#<Account id: 25, subdomain: "bigbangtechnology">]
>> Package.first.subscriptions
=> [#<Subscription id: 1, account_id: 25, package_id: 4>]
>> Account.first.package
Package Load (0.0ms) Mysql::Error: Unknown column 'packages.account_id' in 'where clause': SELECT `packages`.* FROM `packages` WHERE (packages.account_id = 25 )
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'packages.account_id' in 'where clause': SELECT `packages`.* FROM `packages` WHERE (packages.account_id = 25 )
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:320:in `execute'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:595:in `select'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/base.rb:661:in `find_by_sql'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/base.rb:1548:in `find_every'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/base.rb:615:in `find'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:73:in `find_target'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/associations/has_one_through_association.rb:23:in `find_target'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:353:in `load_target'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:112:in `reload'
from /Users/camwest/Sites/yardstickapp/vendor/rails/activerecord/lib/active_record/associations.rb:1219:in `package'
from (irb):22
答案 0 :(得分:1)
从Rails 2.2.1开始,您可以使用委托:
class Account
delegate :package, :to => :subscription
end
答案 1 :(得分:0)
通过阅读文档看起来我找到了答案:
<强>:通过强>
指定用于执行查询的连接模型。以下选项:class_name
和:foreign_key
将被忽略,因为关联使用源反射。您只能通过联接模型上的has_one
或belongs_to
关联使用:through查询。
这告诉我的是,除了订阅模型(我的连接模型)之外,你不能使用:on on has_one。吮!如果有人有任何其他答案,那将是伟大的。