What would a default getter and setter look like in rails?
Asked Answered
I

3

25

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.

Impunity answered 8/1, 2012 at 2:6 Comment(2)
the answer below is correct def tag_list is the accessor, it has to return a tag_list, so return @tag_list. Since ruby returns the last statement always, you can skip the return and just say \@tag_list to return \@tag_list.Conlon
Have you found your solution ? I have a similar problem at #19199179 where my setter is never fired.Messere
P
69

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
Pinafore answered 8/1, 2012 at 2:9 Comment(11)
does it matter what I write for the argument in the setter? or is it just def tag_list=(whatever); @tag_list = whatever; end ?Impunity
@jay: No, it does not matter. Also, this is not Rails-specific, it's a Ruby thing. I edited your question to reflect this.Pinafore
@jay: I updated the answer to add some extra information WHY this works.Pinafore
niklas, sorry if i wasn't clear - i really need to make this work in a rails context. it's an html form that is submitting, and I can't retrieve the values from the attribute tag_list. This answer doesn't work.. I really would like to have tag_list accept from params[:object][:tag_list]Impunity
I think your edits are the right way to go... but I still can't access section_list from other methods in my model. Is it possible there is something else missing that the rails attr_accessor sets?Impunity
@jay: No, actually attr_accessor is not overridden by Rails (so it would be equivalent to my first code sample). Do you say that attr_accessor works okay but not that first code sample?Pinafore
yes it does.. attr_accessor works find, your code does not - tag_list is nil in the case of trying to use your codeImpunity
@jay: I can hardly believe that. Do you try to access the attribute with obj[:tag_list]? This will neither work with attr_accessor nor with the getter and setter I provided (I just tested it in a Rails console to be sure). Can you paste some (please only the relevant) code on pastie.org to show me the issue?Pinafore
Hey Niklas, please know that I am very appreciative of your helping me with this. Thank you very much. I'm trying to be as cooperative as possible. I do not know what code to share. I write exactly the code you wrote into my model, and I have a validation validates :tag_list, :presence => true. THis validation fails - indicating that tag_list never gets read by the controller on form submissionImpunity
@jay: I cannot reproduce this, it should work. See pastie.org/3146488 for a working example under Rails 3.1.3. Sorry but it's late, I don't think I can spend more time on this, so could you please accept this answer? If you have any more problems, you probably want to open a new question with a some real code that causes you problems.Pinafore
Actually there is one more thing that came to my mind: If you are using virtual attributes like these, their values will not be saved to the database, of course, as there is no matching DB column. Maybe you are setting a value, saving, and then loading the entry from some other place. This means that the value of the virtual attribute will be lost. Good night.Pinafore
N
10

When using ActiveRecord, this is the equivalent getter and setter versions:

def tag_list
  read_attribute(:tag_list)
end

def tag_list=(val)
  write_attribute(:tag_list, val)
end

Is this what you were looking for?

Nephro answered 8/1, 2012 at 2:13 Comment(3)
yes - this is kind of, and I'm pretty sure it's part of the way to go.. It almost works, except for some reason tag_list can't be accessed in other methods in my model still. Is there something else that attr_accessor does that is missing here?Impunity
Are you doing tag_list = whatever? Because if so, you're not invoking the setter, you're initializing a local variable. Instead you would do self.tag_list = whatever. Alternatively, if you're having difficulty getting it to read values from the form, check if you're preventing its mass assignment by not adding it to an attr_accessible ... list.Alternate
To note, I had an issue with minitest where I would get "can't write unknown attribute..." errors when using setter methods defined like this (with write_attribute). When I just switched the setter to setting an instance variable named after the virtual attribute, both tests and my web application worked fine. #31999960Kokaras
M
0
Notice the code below is in the [Helpers] path. Helpers are now included for                            
all [Controllers] to work from when instantiated.

module SettergettersHelper

#TODO Wayne 
mattr_accessor :nameport
#TODO Wayne Mattingly the code below was replaced BY ABOVE 
#TODO and not depricatable RAILS 4.2.3

# def nameport
#   @nameport 
# end

# def nameport=(nameport)
#   @nameport = nameport 
#end
end

*Getter from Accounts Controller:*
def index
   @portfolio_name = nameport     
end
*Setter from Portfolio Controller:*
def show
    @portfolio_name = @portfolio_name.portfolio_name #from database call
    SettergettersHelper.nameport =  @portfolio_name # set attribute
end
Monia answered 31/8, 2015 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.