On Mon, Apr 13, 2009 at 6:27 AM, Nick Faraday
<rails-mailing-l...@andreas-s.net> wrote:
>
> 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
> --

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to