On Tue, Apr 14, 2009 at 2:54 AM, Nick Faraday
<rails-mailing-l...@andreas-s.net> wrote:
>
> 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?

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

--~--~---------~--~----~------------~-------~--~----~
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