Hi there, I''ve been trying to figure out if/how the following is possible... @books: array of books @books.each do |book| do stuff... do stuff... do stuff... book["number_of_pages"] = 123 #this is supposed to "inject" the key/value pair end I want to: ========= -> "inject" the key ''number_of_pages'' with the value ''123'' into the hash "book" ("book" IS a hash, right?), for every book in @books -> Then, show in the view: All "Number of pages": <%= @books.collect {|b| [b.number_of_pages] } %> It doesn''t work like this... (-> "undefined method `number_of_pages'' for #<Book:0xb5c022bc>") How can I solve it...? Many thanks for your help! Tom -- 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
... anybody ... ? -- 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mikel Lindsaar
2008-Nov-07 11:07 UTC
Re: Insert key/value pair into hash, then read it out
On Fri, Nov 7, 2008 at 7:28 AM, Tom Ha <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> @books: array of books > @books.each do |book| > do stuff... > do stuff... > do stuff... > book["number_of_pages"] = 123 #this is supposed to "inject" the > key/value pair > end> -> "inject" the key ''number_of_pages'' with the value ''123'' into the hash > "book" ("book" IS a hash, right?), for every book in @booksNo, book is a class, I assume at least. Probably in your models directory you have a book.rb file that has class Book < ActiveRecord::Base <stuff> end -> Then, show in the view: All "Number of pages": <%= @books.collect> {|b| [b.number_of_pages] } %> > > It doesn''t work like this... (-> "undefined method `number_of_pages'' for > #<Book:0xb5c022bc>") >You probably want a column called number_of_pages in your books table Then you can do: book.number_of_pages = 123 book.number_of_pages #=> 123 Hope that helps Mikel -- http://lindsaar.net/ Rails, RSpec and Life blog.... --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Mikel. We''re half way there, I guess:> No, book is a class, I assume at least. > > Probably in your models directory you have a book.rb file that has > > class Book < ActiveRecord::Base > <stuff> > endYes, that''s the case.> You probably want a column called number_of_pages in your books table > > Then you can do: > book.number_of_pages = 123 > book.number_of_pages #=> 123Not really. Actually, I don''t want to store the "number_of_pages" in a database, I''d just lik to add it "on the fly" to each "book" to be able to display it in the view, for each "book". Is this possible in my case? -- 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 7, 2008, at 5:48 AM, Tom Ha wrote:> > Thanks, Mikel. We''re half way there, I guess: > >> No, book is a class, I assume at least. >> >> Probably in your models directory you have a book.rb file that has >> >> class Book < ActiveRecord::Base >> <stuff> >> end > > Yes, that''s the case. > >> You probably want a column called number_of_pages in your books table >> >> Then you can do: >> book.number_of_pages = 123 >> book.number_of_pages #=> 123 > > Not really. Actually, I don''t want to store the "number_of_pages" in a > database, I''d just lik to add it "on the fly" to each "book" to be > able > to display it in the view, for each "book". > > Is this possible in my case?If you add these to the class: def calc_number_of_pages @page_count = (your code to determine page count) end def number_of_pages @page_count end Then you can do: @books.each do |book| book.calc_number_of_pages end Then in your view you can have <%= book.number_of_pages %> and it will do what you''ve described. --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great, thanks a lot! -- 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 To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---