Accessing fields in ActiveRecord


#1

Here is something I found in version 1 of the Agile Web Dev book page
203.

If account is an active record and balance is a field, then you can
access the field as either

account[:balance] = 5
account.balance = 5

The first method is deprecated the book says. However using that method
seems like a good way to abstract your fields. You can have:

fld = :balance
account[fld] = 5

That way suppose you where tyrying to write a sort of framework that
operated on various records. None of the records all had the same field
names. It seems you might want to access fields in that manner for some
such circumstance. Suppose for instance that you had some kind of
framework that did special things to the data on various fields based
on the nameing convention of each field name. You might want to go
iterate through each field in a record and modify it’s data based on
the name of the field. When you get the record initially, you don’t
know what all the fields are in the record as you are looking at the
record in a more abstract way. It seems like at one point I had tried
to do something like that but had some problems, perhaps because either
it was not well supported in ActiveRecord or at the time I was not
fully aware of what was available, but it was a while ago and I am not
sure I remember all the details.


#2

surf wrote:

account[:balance] = 5
account.balance = 5

The first method is deprecated the book says. However using that method
seems like a good way to abstract your fields. You can have:

fld = :balance
account[fld] = 5

This is probably the best way to do that:

field = :balance
account.send(field) if account.respond_to?(field)

The send method calls the “field” method, repsond_to? checks to see if
the object can respond to the “field” method.

Dan M.


#3

I forget exactly, but I think I had a module as shown below. I used
write_attribute and read_attribute, but I wanted the accessable outside
of the record directly which they where not so I used this module and
then included that in my record as shown below.
I had tried some other ways to access the fields besides the usual way,
but they didn’t work well.

I had started to work on this dateing website idea and it all evolved
into this framework and it started to get too pie in the sky. It was
really a learning project for Rails. However the problem I have now is
trying to explain to potential employers what I did and why I did it
that way. I have it all on my lap top but it’s not done and I don’t
have the motivation anymore to mess with it much. I did do some work on
another site that’s online, but it was a small project. I got laid off
from a job where I was doing alot of maintainence programming so I
started to work with Rails and needed a complex project to help me
learn. I went hog wild with ideas and creativiity for awhile, now I am
just trying to find a job.

module SqlInterface
def write_fld(a,v)
write_attribute(a,v)
end

def read_fld(a)
read_attribute(a)
end

various other…

end

require ‘sql_interface’
class Profile < ActiveRecord::Base
include SqlInterface


#4

I wonder what happens when you have a field called “class” or any other
name that is already a method on a base object.

-John


#5

I think you would access it as a symbol that is:

myRec[:class]


#6

When using inheritance in ActiveRecord to automatically assign a
Object’s model type, you need a :type field in your table. This lets
Rails create inheritance based on that field, and essentially creates a
new class. You run into a problem when Rials wants :type to define a
class, but class is a Ruby key word.

My solution is to create a attr_reader and attr_writer to get and set
my Object’s type

app/models/objects.rb

def object_class
self[:type]
end

def object_class=(classin)
self[:type] = classin.to_s
end

Then you can call self.object_class = “MyClass”
and “MyClass” is stored in the :type field, and uses a hierchal
structure of differnet, but similar Object types in one table.