# Using another Model

**URL:** <https://www.ruby-forum.com/t/using-another-model/124788>\
**Category:** Rails\
**Created:** [January 2, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/using-another-model/124788 "2008-01-02T18:02:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [January 2, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/using-another-model/124788/1 "2008-01-02T18:02:44Z")

</div>

Can I access _any_ model from any controller or only the model  
corresponding to the controller?

I’m aware that I can have

class LibraryController \< AC:B

```
 book = Book.new

```

end

but could that be

```
book = Book.find_book_by_ISBN(params[:isbn])

```

or  
book = Book.find\_book\_by\_title\_and\_author(params[:title],  
params[:author)

So that would have to be a class, rather than an instance method?  
I’m not clear how they are different or on how to write them or other  
subtleties.

–  
“Tell me, and I’ll forget. Show me, and I’ll remember.  
Involve me, and I’ll learn.”  
– Marla Jones

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [January 2, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/using-another-model/124788/2 "2008-01-02T18:10:25Z")

</div>

On 2 Jan 2008, at 17:02, Anton J Aylward wrote:

> Can I access _any_ model from any controller or only the model  
> corresponding to the controller?

any model you want. A controller is just a regular ruby class

> or  
> book = Book.find\_book\_by\_title\_and\_author(params[:title],  
> params[:author)

of course. (but you get those for free via dynamic finders)

> So that would have to be a class, rather than an instance method?  
> I’m not clear how they are different or on how to write them or other  
> subtleties.

Not sure what the question is

Fred

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [January 2, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/using-another-model/124788/3 "2008-01-02T18:11:31Z")

</div>

yes, that’s all possible and it must not even be a book,  
you can access any model from any controller (eg the author  
of the book)

a class method by the way is assigned to the class, like  
you example with the finders  
you must call them like Book.method()

a instance method is assigned to the instances of the  
class and can access data of this instance

so if you have a method price\_with\_btw() for your book:  
def price\_with\_btw()  
price + price / 100 \* 19  
end

@book = Book.find(params[:id])  
then  
@book.price\_with\_btw()  
would give you the price, if there’s a ‘price’ column  
in your db

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [January 2, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/using-another-model/124788/4 "2008-01-02T23:35:14Z")

</div>

Thorsten M. said the following on 02/01/08 12:11 PM:

> yes, that’s all possible and it must not even be a book,  
> you can access any model from any controller (eg the author  
> of the book)
> 
> a class method by the way is assigned to the class, like  
> you example with the finders  
> you must call them like Book.method()

OK, but in model/book.rb, how do I write them to make them different  
from instance methods?

> a instance method is assigned to the instances of the  
> class and can access data of this instance

So far that is what I’ve been writing.  
But a class initializer is obviously a class method since you write

```
book = Book.new

```

but its written just like the instance methods.  
So the ruby interpreter ‘just knows’.  
But how do you TELL IT?

I’ve seen some strange syntax with “\<” and “yield self” and lines with  
just “self” but I find it all confusing, and the pickaxe book seems to  
assume you know about this anyway - or at least I don’t find its  
examples helpful compared to some of the code I read.

–  
“Education must precede motivation.” Jim Rohn

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [January 2, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/using-another-model/124788/5 "2008-01-02T23:54:34Z")

</div>

I would like to point out that you were originally using  
Book.find\_book\_by\_isbn where it’s only necessary to use  
Book.find\_by\_isbn.

OK, but in model/book.rb, how do I write them to make them different

> from instance methods?

What are you trying to do? Redefine the methods for the Book model?

The Book class knows what to do when you call any method on it because  
it’s  
inherited from the ActiveRecord::Base class. The AR:B class defines  
these  
methods on itself, and any classes that inherit from it automatically  
inherit these methods as well.

Overriding these methods is not really a good idea unless you know what  
you’re doing.

I’ve seen some strange syntax with “\<” and “yield self” and lines with

> just “self” but I find it all confusing, and the pickaxe book seems to  
> assume you know about this anyway - or at least I don’t find its  
> examples helpful compared to some of the code I read.

I’m assuming you mean \<\< here. \<\< is the concatenation method. You could  
do  
this:

string = "Hello "  
puts string \<\< “world”  
puts string  
and get “Hello world” twice. \<\< modifies anything to the left of it  
(sometimes referred to as a receiver), and then saves it as that object  
again, so the next time you do puts string it will automatically be  
“hello  
world”.

yield is basically an output method. It’ll yield whatever’s passed into  
it,  
in the case you provided it will be self. I’ll need to see this in the  
proper context before I could explain what it does, but I’ll be a bit  
shaky  
on it myself.

self is generally the class that you’re currently in. Say you’ve defined  
a  
method like this in your app/models/book.rb:

def self.find\_all\_hardcovers

end

The self here references the class object, Book. Any method you call  
within  
this new method definition will be called on a class, rather than an  
object  
of that class.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
