考虑到以下模型,我遇到了一个奇怪的情况:
class Collection < ActiveRecord::Base
attr_accessible :name, :season, :year
has_many :collection_items_assocs
has_many :items, :through => :collection_items_assocs
end
class Item < ActiveRecord::Base
attr_accessible :name, :reference, :item_type_id
has_many :pictures
has_one :item_type
end
class CollectionItemsAssoc < ActiveRecord::Base
attr_accessible :collection_id, :item_id
belongs_to :item
belongs_to :collection
end
我可以使用以下代码成功检索与集合关联的项目:
# GET /collections/1
# GET /collections/1.json
def show
@collection = Collection.find(params[:id])
@collection.items = Collection.find(params[:id]).items
respond_to do |format|
format.json { render json: @collection.to_json(:include => {:items => @collection}) }
end
end
但是当我尝试包含这样的分页(对于项目)时
# GET /collections/1
# GET /collections/1.json
def show
@collection = Collection.find(params[:id])
**@collection.items = Collection.find(params[:id]).items.paginate(:page => params[:page],:per_page =>1)**
respond_to do |format|
format.json { render json: @collection.to_json(:include => {:items => @collection}) }
end
end
它适用于以下调用
/retailapp/collections/1?format=json&**page=1**
然后如果我打电话
/retailapp/collections/1?format=json&**page=2**
关联表中的记录CollectionItemsAssoc
被删除
我真的不明白
谢谢你的帮助