sql - Rails find method :select alias as id? -


i have .find method in 1 of rails controller actions - relevant part is:

.find(:all, :select => 'last_name id, last_name name') 

i getting odd behaviour trying alias last_name column id - if alias else, works fine (i can last_name xyz , outputs last name in column called xyz, using populate drop-down need have name in id column, need called 'id').

i should point out output id column, "id":0.

could shed light on need column aliased 'id'?

thanks!

i'm not sure of how can in rails query statement. rails going try , take on id column, casting value returned database id type of column id (presumably integer). that's why id column keeps getting set 0, because "string".to_i #=> 0

however, there way it, once have results back.

since have question tagged rails 3, preferable use new activerelation syntax. can following:

# first, results query, loop through of them. customer.select("last_name 'ln', last_name 'name'").all.collect |c|   # first step of loop attributes hash form   h = c.attributes   # next step create "id" key in hash.   # hash#delete method deletes key/value pair @ key specified , returns value.   # we'll take returned value , assign created "id" key.   h["id"] = h.delete("ln")   # , have call out hash ensure it's returned value collect.   h end 

that hash id value text string value last_name , name value same.

hope helps!


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -