My client asked me to do all the Model.find() in Model class i.e all
database interaction should be in model class and not in controller
I don’t know how to do that
I need to pass details from html to model and do database work and send
back
to view
Uhh, get a new client? If the client knows enough to dictate that level
of
programming, either they don’t need you or you don’t need them.
But less cynically, ask them what they are trying to accomplish by this.
Some
guru told them the “One True Way”? It may be reasonable to require all
SQL be
in the model class. The whole purpose of the find() method is to
program
database access in Ruby without knowing SQL.
The crude, ask no questions response is to take pluck out every find()
call
and a reasonable amount of context in the controller and wrap it in a
method
in the model and call that.
But, I would really try and get some clarification on why and what they
expect
to accomplish by this change. On the face of it, it seems very
unreasonable.
My client asked me to do all the Model.find() in Model class i.e all
database interaction should be in model class and not in controller
I don’t know how to do that
I need to pass details from html to model and do database work and send
back
to view
I need some help
Any sample link or any advice ?
–
Karthik.k
Mobile - +91-9894991640
Write class methods in Model. Call those from Controllers by passing the
arguments.
controller method
def example
User.method_name(arg1,arg2)
end
In User Model
def self.method_name(parm1,param2)
write your find here
end
On Thu, Aug 20, 2009 at 10:03 AM, Jeffrey L. Taylor [email protected]wrote:
to view
Some
But, I would really try and get some clarification on why and what they
expect
to accomplish by this change. On the face of it, it seems very
unreasonable.
HTH,
Jeffrey
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 !@contract.blank? && !@state.blank? && @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”
elsif !@agency.blank? && !@state.blank? && @contract.blank?
@agencies=Agency.all(:joins=>:contracts,
etc
The above method is in controller so i need to write all the below code
in
model
if !@contract.blank? && !@state.blank? && @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”
elsif !@agency.blank? && !@state.blank? && @contract.blank?
@agencies=Agency.all(:joins=>:contracts,
and just retrieve the result and display in html
I need to write a method in model and access it from controller
This is what we need
On Thu, Aug 20, 2009 at 10:03 AM, Jeffrey L. Taylor [email protected]wrote:
[snip]
@agency=params[:agency][:agency_id] #@agency
@contract=params[:agency][:contracts_id] #@contract
@state=params[:agency][:state_id] #@state
if @status==‘deleted’
if !@contract.blank? && !@state.blank? && @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:
So please let me know what is the reason behind this, using in two different
way
self is the implicit receiver, ie calling self.find(:all) is the same
as calling find(:all). Sometimes it is useful to make it obvious what
you are doing
def self.list
…
end
creates a class method. The second example you gave probably wouldn’t
run, unless the model had an instance method called find. More likely
is that you find something like that inside a
class << self
def list
…
end
end
in which case it is identical to the first example