Quoting karthik k <[email protected]>:
> On Thu, Aug 20, 2009 at 10:03 AM, Jeffrey L. Taylor
> <[email protected]>wrote:
[snip]
> Hi Jeffrey
>
> thank you
>
> we have lot of database interaction in the controller
>
> e.g
>
> def searchagency
> @status=params[:status] #...@status
> @agency=params[:agency][:agency_id] #...@agency
> @contract=params[:agency][:contracts_id] #...@contract
> @state=params[:agency][:state_id] #...@state
> if @status=='deleted'
> if [email protected]? && [email protected]? && @agency.blank?
> @agencies=Agency.all(:joins=>:contracts,
> :select=>"distinct agencies.*",:conditions=>["contract_id = ?
> and agencies.state_id =? and agencies.deleted=?",@contract,@state,1])
> render :partial=>"agencysearchdisplay"
[snip]
Use named scopes:
def searchagency
@status=params[:status] #...@status
@agency=params[:agency][:agency_id] #...@agency
@contract=params[:agency][:contracts_id] #...@contract
@state=params[:agency][:state_id] #...@state
if @status=='deleted'
@agencies = Agency.all_deleted(@contract, @state)
if [email protected]? && [email protected]? && @agency.blank?
render :partial=>"agencysearchdisplay
in model class
named_scope :all_deleted, lambda {|contract, state|
{:joins => :contracts,
:select => "distinct agencies.*",
:conditions =>
["contract_id = ? AND agencies.state_id =? AND agencies.deleted=?",
contract, state, 1]
}
HTH,
Jeffrey
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---