[英]Accessing a belongs_to associated model via has_one :through
I can't seem to figure out why this association isn't working: I'm not sure how to describe the situation better than providing a code example: 我似乎无法弄清楚这种关联为什么不起作用:我不确定如何比提供代码示例更好地描述这种情况:
Here is my application environment: 这是我的应用程序环境:
Ruby version 1.8.7 (universal-darwin10.0)
Rails version 2.3.3
And a code sample: 和一个代码示例:
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
As of Rails 2.2.1 you can use delegate: 从Rails 2.2.1开始,您可以使用委托:
class Account
delegate :package, :to => :subscription
end
http://apidock.com/rails/Module/delegate http://apidock.com/rails/Module/delegate
Looks like I found my answer by reading through the documentation: 看起来我通过阅读文档找到了答案:
:through :通过
Specifies a Join Model through which to perform the query. 指定用于执行查询的联接模型。 Options for : class_name
and : foreign_key
are ignored, as the association uses the source reflection. : class_name
和: foreign_key
将被忽略,因为关联使用源反射。 You can only use a :through query through a has_one
or belongs_to
association on the join model. 您只能在联接模型上通过has_one
或belongs_to
关联使用:through查询。
What this tells me is that you can't use :through on has_one on anything but the Subscription model (my join model). 这告诉我的是,除了订阅模型(我的加入模型)以外,您不能在has_one上使用:through。 Sucks! 糟透了 If anyone has any other answers that'd be great. 如果有人有其他答案,那就太好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.