Hi all. I''m trying to DRY out a bit of code that I''ve written. Right now, its something along the lines of: if var="column1" User.find(:all, :conditions =>{:column1=>true}) elsif var="column2" User.find(:all, :conditions =>{:column2=>true}) ... end Now, since I know that var is always going to represent a boolean database column, is it possible to do something along the lines of User.find(:all, :conditions => {:var =>true}) or am I stuck using the if statement? Thanks in advance for your help :) -- 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 3 Jul 2008, at 17:41, Dan __ wrote:> > Hi all. > > I''m trying to DRY out a bit of code that I''ve written. Right now, its > something along the lines of: > > if var="column1" > User.find(:all, :conditions =>{:column1=>true}) > elsif var="column2" > User.find(:all, :conditions =>{:column2=>true}) > ... > end > > Now, since I know that var is always going to represent a boolean > database column, is it possible to do something along the lines of > > User.find(:all, :conditions => {:var =>true}) >Try it :-) Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
THAT is impressive. I like Rails more and more the more I use it :) I had assumed that doing something like this might''ve caused Rails issues, since a variable name could also conceivably be a column name. Thanks for pointing out that I was wrong :) -- 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 -~----------~----~----~----~------~----~------~--~---
A somewhat related question now (and I did test this one out, didn''t assume it was overly complicated this time :) ). Say var = "column1". Is there a way to do something along the lines of @user.var, and have that return the value in column1 for that user? It would improve readability and DRY out my code substantially (Google searches on this topic have proved fruitless, probably because I don''t know the right terms to search for). Thanks in advance for any help (again) :) -- 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 Sat, Jul 5, 2008 at 8:58 AM, Dan __ <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> A somewhat related question now (and I did test this one out, didn''t > assume it was overly complicated this time :) ). Say var = "column1". > Is there a way to do something along the lines of @user.var, and have > that return the value in column1 for that user? It would improve > readability and DRY out my code substantially (Google searches on this > topic have proved fruitless, probably because I don''t know the right > terms to search for).@user.send(var.to_sym) -- http://lindsaar.net/ Rails, RSpec, Puppet 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 -~----------~----~----~----~------~----~------~--~---
> @user.send(var.to_sym)Thanks for the reply :) Is there any way for me to alter a column value accessed in such a way? When I try now, it gives errors on the = sign, and I can''t find anything similar that would allow me to do it. -- 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 Jul 5, 4:01 pm, Dan __ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > @user.send(var.to_sym) > > Thanks for the reply :) Is there any way for me to alter a column value > accessed in such a way? When I try now, it gives errors on the = sign, > and I can''t find anything similar that would allow me to do it.You don''t actually need the to_sym. As far as setting the value goes you just need to call the appropriate method (ie "foo=") Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Jul 5, 4:01�pm, Dan __ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > @user.send(var.to_sym) >> >> Thanks for the reply :) �Is there any way for me to alter a column value >> accessed in such a way? �When I try now, it gives errors on the = sign, >> and I can''t find anything similar that would allow me to do it. > > You don''t actually need the to_sym. As far as setting the value goes > you just need to call the appropriate method (ie "foo=") > > FredI''m not sure I understand. I don''t have a way to call anything like that, because I''m storing the column name in a variable. If I use var = value, that just changes the value of the variable var, and not the column in the database. @user.send(var) = value causes errors, because it returns the value, and not a variable set to the value, like @user.column1 would return. So basically, if var = "column1", how do I do something along the lines of @user.var = value? And I apologize if your last post did answer the question and I just don''t understand. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 5, 6:27 pm, Dan __ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m not sure I understand. I don''t have a way to call anything like > that, because I''m storing the column name in a variable. If I use var > value, that just changes the value of the variable var, and not the > column in the database. -QDnFQlcmCNdgoHlPtYpdqQ@public.gmane.org(var) = value causes errors, because > it returns the value, and not a variable set to the value, like > @user.column1 would return. > > So basically, if var = "column1", how do I do something along the lines > of @user.var = value?The key bit of information is that @user.column = value is just syntactic sugar for "call the method called ''column='' on @user, with parameter value". So @user.send(var+''='', 42) will call the appropriate method with the appropriate value. Fred> > And I apologize if your last post did answer the question and I just > don''t understand. > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You are absolutely brilliant! Thanks very much for your help, its working perfectly now :) -- 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 -~----------~----~----~----~------~----~------~--~---