# Shifting query from Controller to Model

**URL:** <https://www.ruby-forum.com/t/shifting-query-from-controller-to-model/156315>\
**Category:** Rails\
**Created:** [December 24, 2008, 11:11am UTC](https://www.ruby-forum.com/t/shifting-query-from-controller-to-model/156315 "2008-12-24T11:11:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahroon\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@shahroon\_ali](https://www.ruby-forum.com/u/shahroon_ali)\
**Post date:** [December 24, 2008, 11:11am UTC](https://www.ruby-forum.com/t/shifting-query-from-controller-to-model/156315/1 "2008-12-24T11:11:31Z")

</div>

Hi All, I want to shift my Method from Controller to Model, I  
have a  
controller with no Model because there is no table. Now I want to shift  
my  
Method from Controller to Model. The code is here…  
//////////Dashboard Controller////////////////////////  
class DashboardController \< ApplicationController  
layout ‘standard’  
def index  
@dashs = Lead.find\_by\_sql("(SELECT  
opportunities.created\_on,opportunities.created\_by,opportunities.position\_title  
FROM opportunities) UNION DISTINCT  
(SELECT  
customers.created\_on,customers.created\_by,customers.company\_name FROM  
customers) UNION DISTINCT  
(SELECT  
leads.created\_on,leads.created\_by,leads.first\_name FROM leads)  
ORDER BY created\_on DESC")

```
@dashboards = @dashs.paginate :per_page => 3,:page => params[:page]

end

```

end

Now I want to make a Model for Dashboard and shift my queries from  
Controller to Model.

Thanks & Regards,  
Shahroon

---

<div class="post-metadata">

**Author:** ![shahroon\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@shahroon\_ali](https://www.ruby-forum.com/u/shahroon_ali)\
**Post date:** [December 24, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/shifting-query-from-controller-to-model/156315/2 "2008-12-24T16:03:58Z")

</div>

Hi Shahroon

It looks like you are creating a resource for leads, and that you want  
to  
display these leads in the dashboard for your application.

What you can do is create a controller for leads. Then you have a  
resource  
=\> :leads.

In your routes file you can map the route of your application to the  
leads\_controller.

map.root :controller =\> ‘leads’, :action =\> ‘index’

You can also add a named route like so

map.dashboard ‘/dashboard’, controller =\> ‘leads’, :action =\> ‘index’

This will still allow you to use dashboard\_path.

If you want to move the query to your Lead model you can do it like  
this:

in the controller

@dashs = Lead.some\_method\_name

And then in the lead model

def self.some\_method\_name  
.find\_by\_sql("(SELECT  
opportunities.created\_on,opportunities.created\_by,opportunities.position\_title  
FROM opportunities) UNION DISTINCT  
(SELECT  
customers.created\_on,customers.created\_by,customers.company\_name  
FROM customers) UNION DISTINCT  
(SELECT  
leads.created\_on,leads.created\_by,leads.first\_name FROM leads)  
ORDER BY created\_on DESC")

end

or

class \<\< self  
def some\_method\_name  
find\_by\_sql("(SELECT  
opportunities.created\_on,opportunities.created\_by,opportunities.position\_title  
FROM opportunities) UNION DISTINCT  
(SELECT  
customers.created\_on,customers.created\_by,customers.company\_name  
FROM customers) UNION DISTINCT  
(SELECT  
leads.created\_on,leads.created\_by,leads.first\_name FROM leads)  
ORDER BY created\_on DESC")

end  
end

Regards  
Ivor

---

<div class="post-metadata">

**Author:** ![shahroon\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@shahroon\_ali](https://www.ruby-forum.com/u/shahroon_ali)\
**Post date:** [December 26, 2008, 8:11am UTC](https://www.ruby-forum.com/t/shifting-query-from-controller-to-model/156315/3 "2008-12-26T08:11:03Z")

</div>

Thanks a lot Ivor,  
Well this works and in fact I wanna know why  
I  
need mapping in route file cuz I got the results without routing it ,  
look  
what i did  
1\> I placed my method in Leads Model with query  
2\> Then I called it in my dashboard controller

and it worked, now I am facing problem with its pagintion look at the  
lines  
given below

def self.display\_data  
@dashs = Lead.find\_by\_sql("(SELECT  
opportunities.created\_on,opportunities.created\_by,opportunities.position\_title  
FROM opportunities) UNION DISTINCT(SELECT  
customers.created\_on,customers.created\_by,customers.company\_name FROM  
customers) UNION DISTINCT (SELECT  
leads.created\_on,leads.created\_by,leads.first\_name FROM leads)  
ORDER BY created\_on DESC")  
@dashs.paginate :per\_page =\> 3,:page =\> params[:page]  
end  
when i apply this, I get this error

undefined local variable or method `params’

but when I removed the last line then it gives me the records but  
without pagination of course  
just because the pagination method is removed, so could you help me  
regarding this issue.

Thanks again,  
Shahroon
