Find in model

Hi guys

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

Quoting karthik k [email protected]:

Any sample link or any advice ?

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.

HTH,
Jeffrey

Karthik Kantharaj wrote:

Hi guys

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

Thanks
Siva

Controller

def index
@allemp = Employee.list
end

Model Employee

def self.list
find(:all)
end

On Thu, Aug 20, 2009 at 10:16 AM, Rails L. <
[email protected]> wrote:

def self.list
find(:all)
end

Posted via http://www.ruby-forum.com/.

Hi Guys

Thank you


Karthik.k
Mobile - +91-9894991640
http://kkarthikresume.blogspot.com/

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

Guide me


Karthik.k
Mobile - +91-9894991640

On Thu, Aug 20, 2009 at 2:05 PM, Jeffrey L. Taylor
[email protected]wrote:

        :select=>"distinct agencies.*",:conditions=>["contract_id = ?

if @status==‘deleted’
[“contract_id = ? AND agencies.state_id =? AND
agencies.deleted=?”,
contract, state, 1]
}

HTH,
Jeffrey

Hi Jeffrey

Thank you
i will learn about named_scope


Karthik.k
Mobile - +91-9894991640

Quoting karthik k [email protected]:
[snip]

Thank you
i will learn about named_scope

URL of Railscast is http://railscasts.com/. Episode 108.

Quoting karthik k [email protected]:

On Thu, Aug 20, 2009 at 2:05 PM, Jeffrey L. Taylor [email protected]wrote:
[snip]

Hi Jeffrey

Thank you
i will learn about named_scope

There is an excellent Railscast on named_scope. My Internet connection
won’t
stay up long enough to check, but I think the URL is www.railscast.com.

Jeffrey

Quoting karthik k [email protected]:

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:

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 !@contract.blank? && !@state.blank? && @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

On Thu, Aug 20, 2009 at 7:57 PM, Jeffrey L. Taylor
[email protected]wrote:

There is an excellent Railscast on named_scope. My Internet connection
won’t
stay up long enough to check, but I think the URL is www.railscast.com.

Jeffrey

Hi Guys

I tried to use named_scope and i am successful in doing that

I need to know one thing about self

Below used in definition

Model Employee

def self.list
find(:all)
end

In some case it is used inside

Model Employee

def list
self.find(:all)
end

So please let me know what is the reason behind this, using in two
different
way

Guide me


Karthik.k
Mobile - +91-9894991640

On Aug 21, 6:21 am, karthik k [email protected] wrote:

Model Employee

def list
self.find(:all)
end

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

Fred

Model Employee

def self.list
find(:all)
end

This is a class method inside Employee Model. it can also be written as

def Employee.list
find(:all)
end

This kind of methods can be called without the instance of the Model
class.

In some case it is used inside

Model Employee

def list
self.find(:all)
end

This one is Instance method of Model class Employee