我现在想的是……
我有一个装满书籍(条目)的图书馆。每本书都有许多结帐(嵌入文档)。
我想我想做的是,在结帐时,将新的“结帐”作为嵌入文档。签入时,我想编辑结帐并添加一个“date_checked_out”字段...
问题是,每次签入或签出时,我当前的模型/控制器都会创建一个新条目……所以它是双重冗余的……
解决这个问题的最佳方法是什么?需要更多细节?
结帐控制器:
def new
@entry = Entry.find(params[:entry_id])
@checkout = @entry.checkout.new
respond_to do |format|
format.html {render :layout => false}
end
end
def create
@entry = Entry.find(params[:entry_id])
@entry.update_attributes(:checked_out => "Out")
@checkout = @entry.checkout.create!(params[:checkout])
redirect_to "/", :notice => "Book Checked Out!"
end
class Checkout
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
field :checkout_date, :type => Time
field :checkout_date_due, :type => Time
field :book_in, :type => Time, :default => Time.now
field :book_out, :type => Time, :default => Time.now
embedded_in :entries, :inverse_of => :entries
end