Hi All Just pondering how you would write a statement to convert all the output of a database. That is if you had yes/no fields stored as 1''s and 0''s could you write a simple statement to iterate though all of them and change the 1 to a "Yes"? I know you can do it individually but how about all of them? Cheers --~--~---------~--~----~------------~-------~--~----~ 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 Jul 23, 8:58 am, Armitage <rod.middle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All > > Just pondering how you would write a statement to convert all the > output of a database. > > That is if you had yes/no fields stored as 1''s and 0''s could you write > a simple statement to iterate though all of them and change the 1 to a > "Yes"? > > I know you can do it individually but how about all of them?You can do the equivalent of a SQL UPDATE command by using update_all: MyModel.update_all "myfield = Yes", "myfield = 1" BUT, why would you do this? Leaving it as one/zero gives you some free behavior in your model. Let''s say your column is called ''activated'' or something like that. Leave it as 1/0 and you can do: my_model.activated? This will return true if it''s a 1, false otherwise. If you need to display "yes" or "no" somewhere in a view, and that''s why you''re changing the data, then you can write a helper method to do that: def display_activated(model) model.activated == ''1'' ? ''Yes'' : ''No'' end and then in your view: <%= display_activated(@model) %> But again, I''m not sure why you need to change the data, so my suggestions may or may not be appropriate...? Let me know. Jeff www.essentialrails.com - 2-Day Rails training for beginners. Rails made simple. And fun. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Jeff Thanks for your response I don''t think I was terribly clear I am afraid, I dont want to change the output in the database but in the html table I am outputting. I am storing the Yes/No values as Zeroes and Ones and just wondered how you would write some sort of global method to say where you see a ''0'' writer ''No '' Rod On Jul 23, 3:30 pm, Jeff <cohen.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 23, 8:58 am, Armitage <rod.middle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi All > > > Just pondering how you would write a statement to convert all the > > output of a database. > > > That is if you had yes/no fields stored as 1''s and 0''s could you write > > a simple statement to iterate though all of them and change the 1 to a > > "Yes"? > > > I know you can do it individually but how about all of them? > > You can do the equivalent of a SQL UPDATE command by using update_all: > > MyModel.update_all "myfield = Yes", "myfield = 1" > > BUT, why would you do this? Leaving it as one/zero gives you some > free behavior in your model. Let''s say your column is called > ''activated'' or something like that. Leave it as 1/0 and you can do: > > my_model.activated? > > This will return true if it''s a 1, false otherwise. > > If you need to display "yes" or "no" somewhere in a view, and that''s > why you''re changing the data, then you can write a helper method to do > that: > > def display_activated(model) > model.activated == ''1'' ? ''Yes'' : ''No'' > end > > and then in your view: > > <%= display_activated(@model) %> > > But again, I''m not sure why you need to change the data, so my > suggestions may or may not be appropriate...? Let me know. > > Jeffwww.essentialrails.com- 2-Day Rails training for beginners. Rails > made simple. And fun.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
this is exactly what jeff was displaying to you:> If you need to display "yes" or "no" somewhere in a view, and that''s > why you''re changing the data, then you can write a helper method to do > that:here you define the method that will change the ''1'' to a ''yes'' and the ''0'' to a ''no'' string:>> def display_activated(model) >> model.activated == ''1'' ? ''Yes'' : ''No'' >> end >> >> ## u may want to do the middle line as model.activated==false ? ''no'' :''yes'' >> ## if you store 1''s and 0''s in boolean fields ..and here you call the method, to write the ''no'' / ''yes'' according to the @model.activated value:> > <%= display_activated(@model) %> > ## output would be either ''yes'' / ''no'' depending on @modelisn''t this what you want? hth -- 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 -~----------~----~----~----~------~----~------~--~---
Yep you are absolutely right.... <slightly embarrassed> Thanks to both for your input - note to self drink coffee before posting without fully reading Thanks again! On Jul 24, 9:42 am, Shai Rosenfeld <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> this is exactly what jeff was displaying to you: > > > If you need to display "yes" or "no" somewhere in a view, and that''s > > why you''re changing the data, then you can write a helper method to do > > that: > > here you define the method that will change the ''1'' to a ''yes'' and the > ''0'' to a ''no'' string: > > >> def display_activated(model) > >> model.activated == ''1'' ? ''Yes'' : ''No'' > >> end > > >> ## u may want to do the middle line as model.activated==false ? ''no'' :''yes'' > >> ## if you store 1''s and 0''s in boolean fields .. > > and here you call the method, to write the ''no'' / ''yes'' according to the > @model.activated value: > > > > > <%= display_activated(@model) %> > > ## output would be either ''yes'' / ''no'' depending on @model > > isn''t this what you want? > hth > -- > 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-/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 Jul 24, 3:42 am, Shai Rosenfeld <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> this is exactly what jeff was displaying to you: > >> ## u may want to do the middle line as model.activated==false ? ''no'' :''yes'' > >> ## if you store 1''s and 0''s in boolean fields ..You can also shorten that line even further to model.activated? ? ''yes'' : ''no'' That seems more clear, no? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---