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
On 3 Jul 2008, at 17:41, Dan __ wrote:
…
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
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
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)
On Sat, Jul 5, 2008 at 8:58 AM, Dan __
[email protected] 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)
–
Rails, RSpec, Puppet and Life blog…
@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.
On Jul 5, 4:01 pm, Dan __ [email protected] 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
On Jul 5, 6:27 pm, Dan __ [email protected] 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. @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?
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
Frederick C. wrote:
On Jul 5, 4:01�pm, Dan __ [email protected] 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
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. @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.
You are absolutely brilliant! Thanks very much for your help, its
working perfectly now