Active Record - Object Wrapping a Table Row

I'm following through an example provided by DHH in his book, "Agile Web Development with Rails". This example deals with a database with an "orders" table that has column names, "name", "email", "address" and "pay_type".

The Order class is created thusly:

class Order < ActiveRecord::Base end

Later the example describes one way of reading data from the rows of the table as follows:

pos = Order.find(:all, :conditions => "name = 'Dave' and pay_type = 'po'")

Then, the author says, "The result will be an array of all matching rows, each neatly wrapped in an Order object."

It would have been nice if he had gone on to at least describe some of the characteristics of this particular object. He doesn't. I have a feeling that one thing that's important for us to know about this object is that the values for each field in the table row is contained in an instance variable with the name of the column heading. I'm not sure if the raw name of the column heading is massaged in any way, i.e., singular/plural, upper/lower case, etc. I'm also assuming that there are accessor methods for these instance variables. What I'd like to know is whether what I have surmised is, in fact, true. I'd also like to know if there is anything else that I should know about this object. Can someone please fill in the blanks for me?

TIA any input.

     ... doug

The order object is an Active Record object. You can find some comprehensive guides on Active Record here: http://guides.rubyonrails.org/

As it states the result is effectively an array of Order objects. So to get the name of the first object one uses pos[0].name. Alternatively one can do things like pos.each do |p|    # here you can access p.name, p.pay_type etc. end

Colin

As it states the result is effectively an array of Order objects. So to get the name of the first object one uses pos[0].name.

I THINK you are confirming my suspicion. Clearly, the first object (which corresponds to the first table row) is pos[0]. From that I would deduce that pos[0].name is a getter method within the pos[0] object for accessing the name instance variable which apparently contains the value for the name column in the first object (which corresponds to the first row).

contains an instance variable for each column and that instance variable has the same name as the corresponding column. Additionally, the order object has accessor methods (or, at least a getter method; but, I suspect that there is also a setter method) for each of these instance variables.

Anyway, I think that my suspicions are confirmed. Thanks for the input.

      ... doug

I have not looked at that book recently but I would have expected that to be covered rather early on. I am sure that if you turn over the page you will find it.

Colin

> As it states the result is effectively an array of Order objects. So > to get the name of the first object one uses pos[0].name.

From my point of view, the omission in the book is that the order object contains an instance variable for each column and that instance variable has the same name as the corresponding column. Additionally, the order object has accessor methods (or, at least a getter method; but, I suspect that there is also a setter method) for each of these instance variables.

If you want to be entirely correct, the attributes aren't stored as individual instance variables - they're stored together in a hash. That is usually irrelevant though - the important thing is the accessor methods.

Fred

If you want to be entirely correct, the attributes aren't stored as individual instance variables - they're stored together in a hash. That is usually irrelevant though - the important thing is the accessor methods.

Nice to know; but, you're right that we do not really need to know about the internal workings. As you say, we DO need to know about the accessor methods and that was the part that I thought was missing from the book. I have an earlier edition. It may have been picked up by now.

Thanks for the input.

   ... doug

Make sure that the book you are following is written for the version of Rails that you are using (which should be at least version 3), at least to the first two levels (eg 3.2) otherwise you will get horribly confused as the examples will not work.

You might be better working through railstutorial.org, which is free to use online.

Colin