Bruce Balmer
2005-Dec-31 00:00 UTC
[Rails] I don''t even know how to ask the question (4 lines of code)
I create a hash using this line of code @x = Account.find(:all) I then want to go through each element in turn and add a key value pair to the end of it. I thought this would work, but it does not. @x.each do |a| a.merge({"balance"=>50} I used merge because I thought ''a'' would be a hash (how do you find out the type of an object?) I then guess it was an array so I tried both << and + no success. What should I be doing?
Wilson Bilkovich
2005-Dec-31 00:15 UTC
[Rails] I don''t even know how to ask the question (4 lines of code)
On 12/30/05, Bruce Balmer <brucebalmer@mac.com> wrote:> I create a hash using this line of code @x = Account.find(:all) > > I then want to go through each element in turn and add a key value > pair to the end of it. I thought this would work, but it does not. > > @x.each do |a| > > a.merge({"balance"=>50} > > I used merge because I thought ''a'' would be a hash (how do you find > out the type of an object?) > > I then guess it was an array so I tried both << and + no success. > > What should I be doing?Account.find(:all) returns an Array of ActiveRecord instances. You can determine this with inspect. e.g.: logger.debug @x.inspect ..which will place the details in the log file. @x.each do |a| # a is an ActiveRecord object here. # If ''balance'' is already an attribute of the object, you can say: a.balance = 50 end Your code would work if each entry in @x was indeed a Hash, but that''s not the case with the ActiveRecord finders.
Matthew Palmer
2005-Dec-31 00:36 UTC
[Rails] Re: I don''t even know how to ask the question (4 lines of code)
On Fri, Dec 30, 2005 at 04:58:06PM -0700, Bruce Balmer wrote:> I create a hash using this line of code @x = Account.find(:all)By my understanding, @x will be an array after this line.> I then want to go through each element in turn and add a key value > pair to the end of it. I thought this would work, but it does not. > > @x.each do |a| > > a.merge({"balance"=>50}Assuming that what you''re trying to do is set the ''balance'' attribute on all of your accounts to 50, you could try @x.each { |a| a.balance = 50 }> I used merge because I thought ''a'' would be a hashEach ''a'' should be an object of type Account.> (how do you find > out the type of an object?)a.class - Matt -- "[the average computer user] has been served so poorly that he expects his system to crash all the time, and we witness a massive worldwide distribution of bug-ridden software for which we should be deeply ashamed." -- Edsger Dijkstra
Michael Smedberg
2005-Dec-31 00:37 UTC
[Rails] I don''t even know how to ask the question (4 lines of code)
The variable a will hold an Account object. I think you want to do something like Account.find(:all).each do | a | a.balance = 50 end (maybe you want to save the accounts also, etc.) In answer to your other question, you can use functions like is_a? or .class.name to check types of objects: Account.find(:all).each do | a | is_acct = (a.is_a? Account) # is_acct is now true typename = a.class.name # typename holds ''Account'' end On 12/30/05, Bruce Balmer <brucebalmer@mac.com> wrote:> > I create a hash using this line of code @x = Account.find(:all) > > I then want to go through each element in turn and add a key value > pair to the end of it. I thought this would work, but it does not. > > @x.each do |a| > > a.merge({"balance"=>50} > > I used merge because I thought ''a'' would be a hash (how do you find > out the type of an object?) > > I then guess it was an array so I tried both << and + no success. > > What should I be doing? > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20051231/ef8d4b7b/attachment.html
Bruce Balmer
2005-Dec-31 00:51 UTC
[Rails] I don''t even know how to ask the question (4 lines of code)
Thanks. I now guess that a is neither a hash nor an array but rather it is just an object of type object. Is that true? My problem is that a does not currently have a balance attribute and so I wish to extend the object to include one. Is this possible? If not, how can I convert my object to a hash so I can add this attribute ''balance'' that I require. bruce On 30-Dec-05, at 5:36 PM, Michael Smedberg wrote:> The variable a will hold an Account object. I think you want to do > something like > > Account.find(:all).each do | a | > a.balance = 50 > end > > (maybe you want to save the accounts also, etc.) > > In answer to your other question, you can use functions like is_a? > or .class.name to check types of objects: > > Account.find(:all).each do | a | > is_acct = (a.is_a? Account) > # is_acct is now true > typename = a.class.name > # typename holds ''Account'' > end > > > On 12/30/05, Bruce Balmer <brucebalmer@mac.com> wrote: > I create a hash using this line of code @x = Account.find(:all) > > I then want to go through each element in turn and add a key value > pair to the end of it. I thought this would work, but it does not. > > @ x.each do |a| > > a.merge({"balance"=>50} > > I used merge because I thought ''a'' would be a hash (how do you find > out the type of an object?) > > I then guess it was an array so I tried both << and + no success. > > What should I be doing? > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20051231/ce2fe376/attachment.html
Matthew Palmer
2005-Dec-31 01:10 UTC
[Rails] Re: I don''t even know how to ask the question (4 lines of code)
On Fri, Dec 30, 2005 at 05:51:35PM -0700, Bruce Balmer wrote:> Thanks. I now guess that a is neither a hash nor an array but rather > it is just an object of type object. Is that true?No, from the code in your last post, each ''a'' will be an object of type Account, which should be a descendant of ActiveRecord::Base.> My problem is that a does not currently have a balance attribute and > so I wish to extend the object to include one. Is this possible? If > not, how can I convert my object to a hash so I can add this > attribute ''balance'' that I require.Keep well away from the idea of hashes. You don''t want them. If you need to add a new attribute, you can just add it to the database table which backs the Account class (if you want to store that value in the database), or else add an :attr_accessor balance to your class definition, which will create the necessary accessor methods to manipulate the instance variable @balance in your Account objects. If the previous paragraph was complete gibberish to you, you might be well served to pick up a copy of the pickaxe and learn some more Ruby programming (or else my explanatory skills have gone badly downhill lately). I can''t imagine doing much at all with Rails without understanding the underlying Ruby code. - Matt
Bruce Balmer
2005-Dec-31 02:55 UTC
[Rails] Re: I don''t even know how to ask the question (4 lines of code)
Mathew: I did understand what you said (except why I would want to avoid hashes). The real reason I wanted to avoid hashes (my reason) was that I felt that this ought to be possible through a proper object oriented approach. Your answer is wonderful. Exactly what my instincts were telling me I should search for but which my brain could not generate. Now you have said it, your explanation is totally and obvious. Thanks so much. I really appreciate your help with this. bruce On 30-Dec-05, at 6:08 PM, Matthew Palmer wrote:> On Fri, Dec 30, 2005 at 05:51:35PM -0700, Bruce Balmer wrote: >> Thanks. I now guess that a is neither a hash nor an array but rather >> it is just an object of type object. Is that true? > > No, from the code in your last post, each ''a'' will be an object of > type > Account, which should be a descendant of ActiveRecord::Base. > >> My problem is that a does not currently have a balance attribute and >> so I wish to extend the object to include one. Is this possible? If >> not, how can I convert my object to a hash so I can add this >> attribute ''balance'' that I require. > > Keep well away from the idea of hashes. You don''t want them. If > you need > to add a new attribute, you can just add it to the database table > which > backs the Account class (if you want to store that value in the > database), > or else add an :attr_accessor balance to your class definition, > which will > create the necessary accessor methods to manipulate the instance > variable > @balance in your Account objects. > > If the previous paragraph was complete gibberish to you, you might > be well > served to pick up a copy of the pickaxe and learn some more Ruby > programming > (or else my explanatory skills have gone badly downhill lately). I > can''t > imagine doing much at all with Rails without understanding the > underlying > Ruby code. > > - Matt > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Maybe Matching Threads
- counting rows via associations
- A book we may wish to buy
- How can one use GRUFF with locomotive ( I guess there is a wider question here, also)
- Trying to grasp the difference between local variables and instance variables/
- dumping out mysql data only (no structure)