[rspec-users] RSpec get model attribute values by name

James Byrne lists at ruby-forum.com
Fri Jan 30 15:45:13 EST 2009


This is a request for a programming technique:

Given

class Mymodel < ActiveRecord::Base
end

my_instance = Mymodel.new

If I am given a string representation of an attribute "xxx" what is the
most elegant way of passing that string to my_instance to retrieve its
value?  This is probably a common idiom, given what Rails does, but I
cannot seem to find its code in the rails gem at the moment.

So, if anyone knows hoe to do this off the cuff I would appreciate it.
An example of the intended use of this is

  def table_compare(table1, table2, array_of_attributes) do |t1, t2,
aon|
    tint = t1.column_names & t2.column_names
    tfin = tint & aon
    tfin.each do |column|
       return false if t1.column <> t2.column
    end
    return tfin  #could be nil
  end

I want to convert the string value of the column variable into the
attribute name to obtain the value of that attribute.
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list