现在。我有一个问题,我希望有人帮助我解决它,问题的日志就像下面的文字
>> Department.find(EmeReference.find(:all,:select =>:ref_config_id,:conditions=>"emergency_id = 1"))
ActiveRecord::RecordNotFound: Couldn't find Department with ID=0
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1591:in `find_one'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1574:in `find_from_ids_without_cache'
from (__DELEGATION__):2:in `__send__'
from (__DELEGATION__):2:in `find_from_ids_without_cache'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:158:in `find_from_keys'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/primary_key.rb:31:in `miss'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:66:in `hit_or_miss'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:17:in `call'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:17:in `fetch'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/accessor.rb:29:in `get'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:65:in `hit_or_miss'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:18:in `perform'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/primary_key.rb:17:in `perform'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/query/abstract.rb:7:in `perform'
from /usr/lib/ruby/gems/1.8/gems/nkallen-cache-money-0.2.5/lib/cash/finders.rb:29:in `find_from_ids'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:in `find'
from (irb):135
这就是问题!谢谢你,最诚挚的问候!
答案 0 :(得分:4)
Rails自己管理模型之间的关系。你不需要像这样嵌套调用。
EmeReference.find_by_emergency_id(1).department
这将使用EmeReference
检索emergency_id=1
对象。由于您应该已经定义了该模型与Department
之间的关系,因此rails会创建第二个模型名称的虚拟方法,以便您获取它。
你应该看看这两个指南。 Active Record Associations和Active Record Query Interface