我有一个包含许多条目的帐户模型,并且我想仅在一段时间内发生时才加载帐户的条目。每个用户会话的这段时间都不同,所以我的account.rb
:
class Account < ActiveRecord::Base
attr_accessible :code, :detail, :name
attr_accessible :startDate, :endDate # not persisted in db
has_many :entries, :order=>'date1,transref', :conditions => { :date1 => "#{@startDate}".."#{@endDate}" }
def startDate=(sd)
@startDate = sd
end
def startDate
@startDate
end
def endDate=(ed)
@endDate = ed
end
def endDate
@endDate
end
end
还有我的accounts_conttoller.rb:
def show
@account = Account.find(params[:id])
@account.startDate = '2012-02-01' #Actual value to be read from session[]
@account.endDate = '2013-02-01' #Actual value to be read from session[]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @account }
end
end
当我调用"show"
时,@account.entries
是空的,并且使用的 SQL 查询是:
SELECT ... WHERE entries.date1 BETWEEN '' and '' ...
和变成startDate
了endDate
空。我的错误在哪里?