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

  • SEARCH
  • Home
  • 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 8705879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:28:31+00:00 2026-06-13T03:28:31+00:00

If I have a RoR model person.rb as follows: class Person < ActiveRecord::Base attr_accessible

  • 0

If I have a RoR model person.rb as follows:

class Person < ActiveRecord::Base
  attr_accessible :first_name, :last_name

  validates :first_name, presence: true
  validates :last_name, presence: true
end

I don’t seem to be able to do any of the below:

@full_name = @first_name + " " + @last_name

or

def full_name
   @first_name + " " + @last_name 
end

To my understanding, both of those should work with a regular ruby class.

I did a bit of reading and the below seems to be the way to go:

def full_name
    self.first_name + " " + self.last_name
end

I can make this work but I really would like to understand why I can’t seem to be able to reference instance variables in any way (nor create new ones).

Does ActiveRecord::Base do something extremely funny to instance variables? Does it limit a model (class Person in this case) to be nothing more then just a wrapper around what’s in the DB?

I can’t seem to define an attr_accessor either… but I can set first_name and last_name just fine (not only via mass assignment but also p = Person.new; p.first_name = foo)

If anyone could please shed some light on this that would be greatly appreciated.

Many thanks,

  • 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-13T03:28:32+00:00Added an answer on June 13, 2026 at 3:28 am

    ActiveRecord model attributes are stored in an instance variable called @attributes, which is a hash. The getters and setters are defined for you to access this variable.

    By the way, this is the recommended way to concatenate strings in Ruby:

    "#{first_name} #{last_name}"
    

    There is a built-in “to_s” method (on most objects) that will allow you to insert them directly into strings this way. While you couldn’t do this: "string " + 1 without raising an error, you can do this: "string #{1}"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this model in legacy code, with RoR 2.3.11: class Assignment < ActiveRecord::Base belongs_to
How close can I get to defining a model in SQLAlchemy like: class Person(Base):
I currently have the following models/toy.rb file in my RoR project: class ToysPurchased include
Suppose I have this model: class PhotoAlbum(models.Model): title = models.CharField(max_length=128) author = models.CharField(max_length=128) class
I have a Members ActiveRecord model and I want to create a method that
I have code in my model (RoR 3.0.x) that is more or less like
I have some application on RoR with Mongodb database. I use Mongoid mapper. Model
I have created a search action for an existing entity(model/controller/view) in RoR and the
I am using FactoryGirl 3.3.0 with RoR 3.2.3 I have a user model which
I have this RoR app that calls to RAILS_ROOT. When running it using rails

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.