hi! how can i find an ID from a table, with another attribute? i mean, i have the table users, with id, and name, and i want to find the id, where the name is. i know only the way with the id.. @something = User.find(params[:id]) which would be the correct syntax? ty!! -- Posted via http://www.ruby-forum.com/.
Hey :)
All the columns in your database/model have ''finder''
attributes,
thanks to ActiveRecord and inheritance, which means you can do either;
a=Booking.find_by_confirmation_date("2005-06-06 00:00:00")
or even
a=Booking.find_all_by_confirmation_date("2005-06-06 00:00:00")
I hope you get the idea ;) Note, if your name is not
''unqiue'', then
obviously, doing the find_by will return the first name that matches,
which in the database could be random. Your mileage will definitely vary.
Regards
Stef
pipo wrote:> hi!
>
> how can i find an ID from a table, with another attribute? i mean, i
> have the table users, with id, and name, and i want to find the id,
> where the name is.
>
> i know only the way with the id..
>
> @something = User.find(params[:id])
>
> which would be the correct syntax?
>
> ty!!
>
>
pipo wrote:> hi! > > how can i find an ID from a table, with another attribute? i mean, i > have the table users, with id, and name, and i want to find the id, > where the name is. > > i know only the way with the id.. > > @something = User.find(params[:id]) > > which would be the correct syntax? > > ty!!If the name is unique, you could do something like this: @id = Users.find_by_name(@name).id or @id = Users.find(:first, :conditions => [ "name = ?", @name]).id These will only find the first matching record though, so if you have duplicate names you''ll have to take only a slightly different route. (apologies if there''s a slight error or two in the code as it''s late and i''m a bit tired, but the general idea is there) -- Posted via http://www.ruby-forum.com/.