Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord Virtual Attributes treaded as a record attributes

I am running into a problem with to_json not rendering my virtual attributes

class Location < ActiveRecord::Base
    belongs_to :event
    before_create :generate_oid
    validates_associated :event

    attr_accessor :event_oid

    def event_oid
      @event_oid = event.oid
    end
end

event_oid is not part of the array returned by:

Location.first.attributes

This is particularly a problem for me when using to_json that automatically serializes record attributes to jason. to_json omits my virtual attribute.

How can you make a virtual attribute treated as an actual instance attribute?

Edit:

to_json is just an example of a method where having my virtual attribute treated as an actual attribute would be nice.

like image 997
Sean McCleary Avatar asked Oct 13 '09 21:10

Sean McCleary


People also ask

What is a virtual attribute in rails?

What is 'Virtual Attribute'? The Virtual Attribute is a class attribute, which has no representation in the database. It becomes available after object initialization and remains alive while the object itself is available (like the instance methods).

What is virtual attribute in Ruby?

Ruby actually lets you create virtual attributes this way, which keeps you from having to manually create getter and setter methods as given below, attr_reader :title # getter. attr_writer :title # setter. attr_accessor :title # both getter and setter.

What is a virtual attribute?

A Virtual Attribute is a type of AttributeTypes in which the Attribute Value are not actually stored in the Back-end but are rather dynamically generated in some manner. Virtual Attributes may or may NOT be part of the entry's defined ObjectClass Type and sometimes any ObjectClass Type.

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


2 Answers

You want to modify the attributes hash. There's a little extra code here to ensure the attributes you care about are ready to be used with to_json or another method that depends on attributes on object load.

class Location < ActiveRecord::Base
    belongs_to :event
    before_create :generate_oid
    validates_associated :event

    after_save :event_oid

    attr_accessor :event_oid

    def event_oid
      @event_oid = @attributes["event_oid"] = event.oid if event.nil?
    end       

    def after_initialize
      event_oid
    end


end

to_json and a lot of other methods that generate lists of things based on an objects attributes. Which is populated on object initialization with database tables and names, unfortunately instance variables do not update this hash.

P.S. this isn't very DRY if you have a number of attributes you want to use in this manner. You could use an Array of symbols, deterministic method names and a class_eval block to apply this process to multiple symbols at once.

Warning

We're messing with rails internals here. There's no telling how it could cause other things to fail. I haven't tested more than save and to_json, both of which work when the attribute hash contains keys that are not also column names. So use it at your own risk.

like image 101
EmFi Avatar answered Nov 15 '22 17:11

EmFi


how about to_json(:methods => [:event_oid]), does that work?

like image 30
Omar Qureshi Avatar answered Nov 15 '22 16:11

Omar Qureshi