Creating Methods in the Model?


#1

Hey Everyone.

First day diving in to ROR and Ruby…

My question is how do you define methods with in the model class and/or
should I even be doing this?

It is my understanding that you should put as much of the business logic
into the model as possible. I want to do some data manipulation before
I things are submitted to the database

IE (Create a variable out of two submitted via form, as well as some
other things.)

Should this all be done in the model? If so how do I declare and call
these methods?

def method_name
def self.method_name
def class_name.method_name

don’t seem to work for me?

should I define them in the model and call them in controller? This
doesn’t seem right to me?

Any Help would be appropriated.

-N


#2

On Mon, Apr 13, 2009 at 6:27 AM, Nick Faraday
removed_email_address@domain.invalid wrote:

I things are submitted to the database

don’t seem to work for me?

should I define them in the model and call them in controller? This
doesn’t seem right to me?

Any Help would be appropriated.

-N

Yes you should be defining methods inside your model.
Models are just classes and so you create methods just like any class.

To expand on your example of a ‘variable’ of two submitted via form:

class User < ActiveRecord::Model
def name
“#{firstname} #{lastname}”
end
end

This gives you method which will combine firstname and lastname (which
would be in the database) into a name

Usage:
user = User.new(:firstname => ‘John’, :lastname => ‘Doe’)
user.name #=> “John D.”

There is nothing wrong with defining these and calling them from a
controller, or view and not sure why that doesn’t seem right to you?
You mention that the methods you have tried don’t work for you, if
you’re still battling, give us some code to look at.

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain


#3

This post should help

http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

On Apr 13, 11:20 am, Andrew T. removed_email_address@domain.invalid


#4

On Tue, Apr 14, 2009 at 2:54 AM, Nick Faraday
removed_email_address@domain.invalid wrote:

return :permalink
end

end

The reason I don’t want to call it in the controller is there is no
reason to (right?), other than I can’t figure out how to call a
self.method in ruby. It is just one more line of code in the controller
that I don’t need.

Is this the right way of thinking?

Don’t use pseudo-ruby, just write ruby otherwise we can’t tell if
you’re making a mistake.
Don’t create a variable called permalink, create a method called
permalink

class User < ActiveRecord::Base
validates…

def permalink
#Do what you need to create the permalink and return it
end
end

There is no reason to validate the permalink because you’re creating
it dynamically. Just validate the presence of name and city so you
have both available to build your permalink
Now whenever you call user.permalink, you’ll get what you need.

self.method creates a method on the User object so that you get
User.method instead of user.method (which is called on the instance of
the object)

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain


#5

Ok, still not getting it…

Here is what I would like to do: (pseudo-ruby)

class User < ActiveRecord::Base

Input from user

validates_presence_of :name, :message => “Your name can’t be blank”
validates_presence_of :city

#User defined Method
:permalink => create_permalink :name, :city

validates_presence_of :permalink

#Method Definition
def create_permalink(name, city)
… permalink code
return :permalink
end

end

The reason I don’t want to call it in the controller is there is no
reason to (right?), other than I can’t figure out how to call a
self.method in ruby. It is just one more line of code in the controller
that I don’t need.

Is this the right way of thinking?