Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non persistent ActiveRecord model attributes

I want to add to an existing model some attributes that need not be persisted, or even mapped to a database column. Is there a solution to specify such thing ?

like image 562
Radu Stoenescu Avatar asked Aug 16 '12 12:08

Radu Stoenescu


People also ask

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is an ActiveRecord object?

In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic as part of the object will educate users of that object on how to write to and read from the database.

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

Of course use good old ruby's attr_accessor. In your model:

attr_accessor :foo, :bar 

You'll be able to do:

object.foo = 'baz' object.foo #=> 'baz' 
like image 150
apneadiving Avatar answered Sep 19 '22 08:09

apneadiving