Using another Model

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.

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

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)

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

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

Thorsten Mueller 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.

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.