Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Controller/Model methods

I am watching Code School Rails testing course. There is an instance of the class zombie. The zombie model has a method:

def avatar_url
  ...
end

Within the test, .rb file has the following:

z.avatar_url
  1. When I call a method like this, how does Rails distinguish if I'm calling a controller or model method? I hadn't thought of calling a model method from other than a controller, and only like Model.method and not object.method.
  2. If both my controller and my model have a method with the same name, how would Rails know which one to call?

Update:

Lets take the class String as example, it is not a model, right?

So I could say:

s = String.new
s.capitalize

If this call doesn't go to a model and not to a controller, where does it go then? Where would a class like String be defined in the Rails directory?

like image 620
Linus Avatar asked Dec 06 '22 05:12

Linus


2 Answers

A method inside a controller can only be called via URL.
Example:

/things/super_action

Should call the def super_action inside ThingsController.

As for Model methods, they can be accessed anywhere. Just note if they are instance or class methods:

Model.ultra_method

This is a class method call, it is probably defined as def self.ultra_method.

m = Model.new
m.instance_method

This is a instance method call, and it is probably defined as def ultra_method.

UPDATE

String is a core class of ruby language. As is Array, Number, etc. In your example you are creating an instance of String and calling an instance method of the String class.

like image 96
MurifoX Avatar answered Dec 19 '22 11:12

MurifoX


It seems like you're new to Ruby as well as Rails. In Ruby, a class is sort of like a description of a type of object (although the class itself is an object, too). Whenever there is a class defined, you can create new instances of it, as with String.new. Note that classes always have capitalized names.

Class methods are methods that work on the class itself. You can tell when a method is a class method because it will be attached to the capitalized name of the class (just like String.new). On the other hand, instance methods only work on an instance of the class, not on the class itself (eg str = String.new; str.capitalize!). Usually there are more instance methods than class methods, because instances are the things that you're actually working with (new is the most common class method you'll see).

As others have mentioned here, String is not a Rails model; it's a basic Ruby class. When you're working in Rails, you have access to all the regular Ruby classes as well as other classes and methods that are defined within Rails' source code. So String is not defined in Rails itself, but Rails does provide some useful instance methods for strings (eg str.to_date).

A model in Rails is really just a Ruby class. To understand the workings of a model, you should make sure you understand how Ruby classes work. What makes Rails models special is that they inherit from a class defined in Rails' source code known as ActiveRecord (any class in Ruby can inherit from another class, this is just one example of that). ActiveRecord has a number of class and instance methods, which are also available to your models because they inherit from ActiveRecord. For example, if you have a class (model) called Person, you can automatically use the Person.find(id) class method to look up a particular instance of the Person class in the database. You also have the person.save instance method to save the instance to the database.

All of this was confusing to me when I first started, so my best advice is to familiarize yourself with Ruby as you learn Rails.

like image 28
grandinero Avatar answered Dec 19 '22 12:12

grandinero