我有以下模型设置活动记录4(映射遗留数据库)
class Page < ActiveRecord::Base self.table_name = "page" self.primary_key = "page_id" has_many :content,foreign_key: 'content_page_id',class_name: 'PageContent' end class PageContent < ActiveRecord::Base self.table_name = "page_content" self.primary_key = "content_id" belongs_to :pages,foreign_key: 'page_id',class_name: 'Page' end
以下工作正常….
page = Page.first page.content.first.content_id => 17 page.content.second.content_id => 18
但是我想能够循环所有的项目,如此
page.content.each do |item| item.content_id end
但它只返回整个集合而不是单个字段
=> [#<PageContent content_id: 17,content_text: 'hello',content_order: 1>,#<PageContent content_id: 18,content_text: 'world',content_order: 2>]
看来它是一个ActiveRecord :: Associations :: CollectionProxy
page.content.class => ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PageContent
有人有任何想法吗?
干杯