# Method models and complex querries

**URL:** <https://www.ruby-forum.com/t/method-models-and-complex-querries/146641>\
**Category:** Rails\
**Created:** [August 23, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/method-models-and-complex-querries/146641 "2008-08-23T12:47:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![idk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e9a140/32.png) [@idk](https://www.ruby-forum.com/u/idk)\
**Post date:** [August 23, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/method-models-and-complex-querries/146641/1 "2008-08-23T12:47:32Z")

</div>

Hello, this is a repost, something seemed to go wrong last time I tried  
to post…

Anyway I got this model:  
class Command \< ActiveRecord::Base  
has\_many :executions

def with\_extension(ext)  
Extension.all(:include =\> :command,  
:conditions =\> { :suffix =\> ext})  
end  
end

And this code in a controller method  
@commands = Command.with\_extension(‘txt’)  
But I get this error then  
undefined method `with\_extension’ for #Class:0xb6eaecbc

I’m obviously doing something wrong, but since I’m not very experienced  
with ruby, I don’t know what it is…

Another problem I have is that I’m not sure the method code is right.  
This is the extensions schema:  
create\_table “extensions”, :id =\> false, :force =\> true do |t|  
t.string “suffix”, :null =\> false  
t.integer “command\_id”, :null =\> false  
end

I want to select all commands which is mapped to a specific suffix.

Niklas U.  
thanks you for your  
time and wishes you  
and everybody  
Happy programming!

---

<div class="post-metadata">

**Author:** ![idk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e9a140/32.png) [@idk](https://www.ruby-forum.com/u/idk)\
**Post date:** [August 23, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/method-models-and-complex-querries/146641/2 "2008-08-23T15:35:07Z")

</div>

On Aug 23, 5:47 am, Niklas U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> This is a method on an instance,

> end
> 
> And this code in a controller method  
> @commands = Command.with\_extension(‘txt’)

But here it is being called for the class.

try:

def self.with\_extension  
…  
end

in the Command class.

However, since the with\_extension method only really interacts with  
the Extension class I think that would be a better place for it.

class Extension \< ActiveRecord::Base  
def self.all\_with\_extension(ext)  
Extension.all(…) # code to find all required extensions  
end  
end

Allan

---

<div class="post-metadata">

**Author:** ![idk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e9a140/32.png) [@idk](https://www.ruby-forum.com/u/idk)\
**Post date:** [August 23, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/method-models-and-complex-querries/146641/3 "2008-08-23T17:31:09Z")

</div>

Allan wrote:

> This is a method on an instance,

Hmm, ok.

> > And this code in a controller method  
> > @commands = Command.with\_extension(‘txt’)
> 
> try:
> 
> def self.with\_extension  
> …  
> end

I did, but I still got the same error…

> However, since the with\_extension method only really interacts with  
> the Extension class I think that would be a better place for it.

My second question was how to perform a complex query so  
it returned all commands which have a command:suffix mapping.

I could do it with code something like this, but it it awfully  
inefficient:  
Extension.all(:include =\> :command,  
:conditions =\> { :suffix =\> ext}).map { |e|  
e.command }

I don’t think that belongs in the extension class.
