Forum: Ruby on Rails retrive data from table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
xxmithila (Guest)
on 2008-10-31 02:09
(Received via mailing list)
hi..

   how can I retrive data from a specific row of table by giving two
or more properties of that row?
examlpe I want to get data which   ID = 23  and NAME = "xx" like

thankx
Matt H. (Guest)
on 2008-10-31 02:54
(Received via mailing list)
xxmithila wrote:
> hi..
>
>    how can I retrive data from a specific row of table by giving two
> or more properties of that row?
> examlpe I want to get data which   ID = 23  and NAME = "xx" like

As long as your table is made up of Models, you can do something like:

@mydata = ModelName.find('23', :conditions => "NAME = 'xxx'" )

HTH

Matt
Bill W. (Guest)
on 2008-10-31 04:13
(Received via mailing list)
Hi,

xxmithila wrote:

> how can I retrive data from a specific row of table
> by giving two or more properties of that row?
> examlpe I want to get data which
> ID = 23  and NAME = "xx" like

If  you know the ID, you don't need anything else.  IDs are unique so
the
query on the second field just eats cycles with no upside.  Assuming
this
was just a poorly thought-out example...

two or more properties are queried, in the most general case, with:
Model.find(all, :conditions => ["first_condition =? && second_condition
=
?", first_param, second_param])

Use the find_by helpers for more readable / idiomatic code.

HTH,
Bill
mithila karunarathna (Guest)
on 2008-11-01 03:07
(Received via mailing list)
On Fri, Oct 31, 2008 at 8:42 AM, Bill W.
<removed_email_address@domain.invalid>wrote:

> If  you know the ID, you don't need anything else.  IDs are unique so the
> Bill
>
>
> hoooo
>
@mydata = ModelName.find('23', :conditions => "NAME = 'xxx'" )
but this command return error
"

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

"
I know that code is correct but it return error. What is the reson for
this?
 xxmithila
mithila karunarathna (Guest)
on 2008-11-01 06:15
(Received via mailing list)
On Sat, Nov 1, 2008 at 6:36 AM, mithila karunarathna
<removed_email_address@domain.invalid>wrote:

>> > by giving two or more properties of that row?
>>
> "
>
>
thankx friends I got the answer...
thankx again
This topic is locked and can not be replied to.