Forum: Ruby on Rails Creating Methods in the Model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
30e3b57c5caefdb4c9e1b43696bdb3d5?d=identicon&s=25 Nick Faraday (nick_faraday)
on 2009-04-13 06:27
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-13 08:22
(Received via mailing list)
On Mon, Apr 13, 2009 at 6:27 AM, Nick Faraday
<rails-mailing-list@andreas-s.net> 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 Doe"

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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Ram (Guest)
on 2009-04-13 10:37
(Received via mailing list)
This post should help

http://weblog.jamisbuck.org/2006/10/18/skinny-cont...

On Apr 13, 11:20 am, Andrew Timberlake <and...@andrewtimberlake.com>
30e3b57c5caefdb4c9e1b43696bdb3d5?d=identicon&s=25 Nick Faraday (nick_faraday)
on 2009-04-14 02:54
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?
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-14 05:16
(Received via mailing list)
On Tue, Apr 14, 2009 at 2:54 AM, Nick Faraday
<rails-mailing-list@andreas-s.net> 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 Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
This topic is locked and can not be replied to.