create_table "areas", :primary_key => "ndc", :force => true do |t|
t.string "townname", :limit => 256, :null => false
end
create_table "books", :primary_key => "ndc", :force => true do |t|
t.integer "booked", :null => false
t.integer "free", :null => false
end
class Book < ActiveRecord::Base
self.primary_key = "ndc"
has_one :area, :foreign_key => 'ndc'
end
class Area < ActiveRecord::Base
self.primary_key = "ndc"
belongs_to :book , :foreign_key => 'ndc'
end
控制器中的我有这样的代码
@books = Book.paginate :page => params[:page] || 1, :per_page => 10
@books.each do |booking|
p booking.area
p booking
end
在生产模式下不起作用,booking.area是零对象。它可以是什么?
如果config.cache_classes = true
,则区域为nil所以booking.area会生成此类查询
如果cashe_classes = true
SELECT areas
。* FROM areas
WHERE(areas
。ndc = NULL)LIMIT 1
但没有兑现课程
SELECT areas
。* FROM areas
WHERE(areas
。ndc = 30)LIMIT 1
通过删除belongs_to:book,:foreign_key =&gt;进行修复来自地区的'ndc'。
答案 0 :(得分:2)
您的areas
表需要book_id
整数字段才能与books表的主键匹配。