I have an Account model that has many Entries, and I want to load the Account's entries only when it happens within a period. This period is different for each user session so my 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
And my 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
When i invoke "show"
, @account.entries
is empty and the SQL query used was:
SELECT ... WHERE entries.date1 BETWEEN '' and '' ...
The startDate
and endDate
became empty. Where was my mistake?