Forum: Ruby on Rails Adding model attribute without db field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ross A. (Guest)
on 2005-12-17 03:04
I need to add an attribute to a model but there is no need to add a
corresponding database field. The model is a child of ActiveRecord and
it corresponds to a table in my DB but when I add it in the usual Ruby
way, I get a 'number of redirects exceeded' error.

Here is my code;

The model;

   class Category < ActiveRecord::Base
     attr :status

The controlller;

    if params[:id] then @category = Category.find(params[:id])
    else @category = Category.find_by_default_category('1')
    @category.status = 1

The view (just to access it)

<%  for category in @categories %>
<%= category.status %>

So, is there a way to add an attribute to an existing table based model?
Chris H. (Guest)
on 2005-12-17 06:05
(Received via mailing list)
class Category < ActiveRecord::Base
  attr_accessor :status

should do the trick

or you could do it longhand

def status

def status=(val)
  @status = val
This topic is locked and can not be replied to.