Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7780637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:57:48+00:00 2026-06-01T18:57:48+00:00

I understand that it is best practice to refactor as much of my code

  • 0

I understand that it is best practice to refactor as much of my code as possible into the models, however I am relatively new to rails, and programming as a whole. One of the concepts that seems to be causing me a little trouble is the nature of models, and understanding the scope or the availability of methods and variables.

First of all with a typical method written in the model what are the limitations (scope) that your method can be called on? How does the .self aspect work? What controllers / views have access to methods defined in the model?

I understand that these are rather basic principles but I believe that my “assumptions” with regard to this are causing me all manner of problems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:57:50+00:00Added an answer on June 1, 2026 at 6:57 pm

    In Model-View-Controller (MVC):

    • A model holds your data, and any functionality closely related to your data (low level logic)
    • A controller holds your business logic (high level application logic)
    • A view holds your presentation layer (user interface)

    Views have access to any public model methods. (Note: all ruby methods are public by default.) Of course, the model object must be instantiated first in the appropriate controller method, and must be instance variables (i.e. @person) and not local variables (i.e. person) in the controller.

    Controllers also have access to any public model methods.

    Protected methods limit access to within the class or within any of its children. Private methods limit access to within the class, only.

    It appears to me that class methods, i.e. def Person.some_method ..., are visible anywhere whether or not they are defined as public, protected, or private, although this is counter-intuitive.


    Regarding your question about self… You can use self for all calls to the model’s own methods from inside that model, and you won’t go wrong.

    e.g. for Person model having first_name and last_name columns:

    class Person < ActiveRecord::Base
      def full_name
        "#{self.first_name} #{self.last_name}"
      end
    
      def parse_name full
        self.first_name, self.last_name = full.split
      end
    end
    

    However, that’s overkill. You actually don’t need to use self for retrieving attributes in ActiveRecord, only for setting attributes, so the following is fine:

    class Person < ActiveRecord::Base
      def full_name
        "#{first_name} #{last_name}"
      end
    
      def parse_name full
        self.first_name, self.last_name = full.split
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that BigDecimal is recommended best practice for representing monetary values in Java.
When sending HTML e-mail, I understand that it's a best practice to send a
below content is taken from Best practice: Writing efficient code but i didn't understand
I understand that the best practice now with Django 1.4 is to store all
I understand that JavaScript isn't really made to open and save files. The best
i am trying to understand what is the best practice for defining and organizing
I understand that client side code must be readable from the browser but I
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.