Jump to content

The ultimate community for Ruby on Rails developers.


Photo

add custom commands available in rails console


  • Please log in to reply
1 reply to this topic

#1 ramborp

ramborp

    Passenger

  • Members
  • 1 posts

Posted 07 February 2014 - 06:06 AM

Hi,

 

I'd like to create some custom methods that are available through the rails console and was wondering how to go about that given the methods will be Model independent. I tried adding a function to ApplicationController but that did not work.  For instance, if I wanted to alias Dir.pwd as 'pwd' ,where would I add the method to interprete the syntax?

 

Thanks for you thoughts,

 

Rob



#2 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 07 February 2014 - 05:55 PM

Hi

 

Welcome to RailsForum.

 

I would encourage adding these methods to the model so they're available on a record or scope basis.

 

Eg;

 

If you have a first_name and last_name row in your User table you could create a full_name method in your User.rb file which contains something like...

def full_name
    "#{first_name} #{last_name}"
end

Hopefully this makes sense! If you provide us with something you're trying to create from your code we'll be able to help you more!

 

 

:)


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users