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 7044339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:23:53+00:00 2026-05-28T02:23:53+00:00

I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list

  • 0

I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object.

If I use attr_accessor :tag_list I can, in the model, perform actions on tag_list to pull and manipulate data from the form.

What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG:

def tag_list
    #what goes here
end

FYI I have tried

 def tag_list
     @tag_list
 end

This does NOT work.

  • 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-05-28T02:23:54+00:00Added an answer on May 28, 2026 at 2:23 am

    attr_accessor is a built-in Ruby method and has no special meaning in the context ActiveRecord. attr_accessor :tag_list is basically equivalent to this code:

    # getter
    def tag_list
      @tag_list
    end
    
    # setter
    def tag_list=(val)
      @tag_list = val
    end
    

    In ActiveRecord models, however, it could be that you want something like this:

    def tag_list
      self[:tag_list]
    end
    
    def tag_list=(val)
      self[:tag_list] = val
    end
    

    There is a slight difference: With the first method, obj[:tag_list] doesn’t use the same storage as your getter and setter. With the latter, it does.

    Explanation of the getter/setter concept

    In Ruby, the following two lines of code are equivalent

    thing.blabla
    thing.blabla()
    

    Both call the method blabla of the object thing and evaluate to the last expression evaluated within that method. This means, you also don’t need a return statement in the case of the above getter method, because the method simply returns the last expression in the method (@tag_list, the value of the instance variable).

    Also, those two lines of code are equivalent:

    thing.blabla=("abc")
    thing.blabla = "abc"
    

    Both call the method blabla= of the object thing. The special name with the = character can be used like any other method name.

    The fact that attributes, as they are sometimes called, are in fact plain methods, you can also use some special logic transformed on the values before returning or accepting them. Example:

    def price_in_dollar
      @price_in_euro * 0.78597815
    end
    
    def price_in_dollar=(val)
      @price_in_euro = val / 0.78597815
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that with mysql you can write SQL statements into a .sql file
I know that you can insert multiple rows at once, is there a way
I know that I can share files using Shared Folders in Virtual PC, but
I know that I can write: foo = 'bar' def update_foo(): global foo foo
Is there any way to check if command exists? I know that I can
I know that I can write a Ruby case statement to check a match
I know that can co-exists with web form no problem as Hanselman mention here
I know that it's a subject that can raise a lot of debate, but
I know that I can do something like $int = (int)99; //(int) has a
I know that JTable can sort by a single column. But is it possible

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.