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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
The order object is an Active Record object. You can find some comprehensive guides on Active Record here: http://guides.rubyonrails.org/ -- Oscar Del Ben Sent with Sparrow (http://www.sparrowmailapp.com/?sig) On Saturday, July 7, 2012 at 9:29 AM, Doug Jolley wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On 7 July 2012 17:29, Doug Jolley <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
> 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). 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. Anyway, I think that my suspicions are confirmed. Thanks for the input. ... doug -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On 7 July 2012 19:26, Doug Jolley <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> 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). > > 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On Jul 7, 7:26 pm, Doug Jolley <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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> Anyway, I think that my suspicions are confirmed. Thanks for the input. > > ... doug > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
> 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On 8 July 2012 19:57, Doug Jolley <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.