# \[newb\] Find vs Find :all question

**URL:** <https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738>\
**Category:** Rails\
**Created:** [February 14, 2006, 2:56am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738 "2006-02-14T02:56:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 2:56am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/1 "2006-02-14T02:56:17Z")

</div>

I’m playing out with writing an online store.

So far it has accounts, addressbooks, and orders, and those 3 are tied  
together with an customer ID. I also have orderDetails, which are  
associated to the order via an order number. See models below…

When I do the following I can access the orderDetails information just  
fine:

```
@order = Order.find(params[:id]) # i pass it a customer id

```

and i could access the orderDetails by doing this:

```
<%for detail in @order.orderDetails %>
	item #: <%=detail.itemId%><br>etc etc
<%end%>

```

However, not all customers have orders, so it sometimes would return  
RecordNotFound error.

So, i changed it to:

```
@order = Order.find(:all, :conditions => ["customerId = ?", 

```

params[‘customerId’]])

which still works. But, now i cannot access the orderDetails the same  
way, it says:  
undefined method `orderDetails’ for []:Array

Have no clue what I’m doing wrong.

Does just plain old find return something different than find :all?

Here are my models:

* * *

class Account \< ActiveRecord::Base  
set\_primary\_key ‘customerId’  
has\_many :addressbooks, :foreign\_key =\> ‘customerId’  
has\_many :orders, :foreign\_key =\> ‘customerId’  
end

class Addressbook \< ActiveRecord::Base  
belongs\_to :account  
end

class Order \< ActiveRecord::Base  
set\_primary\_key “customerId”  
belongs\_to :account  
has\_many :orderDetails, :foreign\_key =\> ‘orderNumber’, :finder\_sql =\>  
'SELECT \* ’ +  
'FROM orderDetails od ’ +  
‘WHERE od.orderNumber = #{orderNumber}’  
end

class OrderDetail \< ActiveRecord::Base  
set\_table\_name “orderDetails”  
belongs\_to :order  
end

* * *

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 3:10am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/2 "2006-02-14T03:10:34Z")

</div>

Find(:all) returns an array of multiple records. Try using find  
(:first) instead, which will return the first record it comes across  
matching your criteria.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 3:12am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/3 "2006-02-14T03:12:05Z")

</div>

David C. wrote:

> I’m playing out with writing an online store.
> 
> So far it has accounts, addressbooks, and orders, and those 3 are tied  
> together with an customer ID. I also have orderDetails, which are  
> associated to the order via an order number. See models below…
> 
> When I do the following I can access the orderDetails information just  
> fine:
> 
> ```
> @order = Order.find(params[:id]) # i pass it a customer id
> 
> ```
> 
> and i could access the orderDetails by doing this:
> 
> \<%for detail in @order.orderDetails %\>  
> item #: \<%=detail.itemId%\>  
> etc etc  
> \<%end%\>
> 
> However, not all customers have orders, so it sometimes would return  
> RecordNotFound error.
> 
> So, i changed it to:
> 
> ```
> @order = Order.find(:all, :conditions => ["customerId = ?", 
> 
> ```
> 
> params[‘customerId’]])
> 
> which still works. But, now i cannot access the orderDetails the same  
> way, it says:  
> undefined method `orderDetails’ for []:Array
> 
> Have no clue what I’m doing wrong.
> 
> Does just plain old find return something different than find :all?

When you do Order.find(:all, …) you will get an array of ActiveRecord  
objects, which is why you are getting an undefined method error.

If you only want to find one record, but pass it conditions, use this:

@order = Order.find(:first, …)

Also, your find condition may have a problem in that customerId would be  
an integer, but using the ‘customerId = ?’ style will (I believe) quote  
the argument, thus giving a WHERE customerId = ‘5’ for example.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 3:58am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/4 "2006-02-14T03:58:00Z")

</div>

@order = Order.find(params[:id]) is the same idea as Order.find  
(:first, :conditions =\> “id = #{params[:id]}”). When you’re searching  
by ID, you’re searching for a unique record, which will return the  
actual record instead of an array of records.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 4:15am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/5 "2006-02-14T04:15:59Z")

</div>

Dylan M. wrote:

> @order = Order.find(params[:id]) is the same idea as Order.find  
> (:first, :conditions =\> “id = #{params[:id]}”). When you’re searching  
> by ID, you’re searching for a unique record, which will return the  
> actual record instead of an array of records.

So how do I access the records in the array? Having trouble finding the  
syntax for that.

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 4:25am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/6 "2006-02-14T04:25:47Z")

</div>

The same way you were trying to iterate through your orderDetails.

\<% @orders = Order.find(:all, :conditions…) %\>  
\<% for order in @orders %\>  
\<% for detail in order.orderDetails %\>  
item #: \<%=detail.itemId%\>  
etc etc  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 14, 2006, 3:40am UTC](https://www.ruby-forum.com/t/newb-find-vs-find-all-question/53738/7 "2006-02-14T03:40:02Z")

</div>

> When you do Order.find(:all, …) you will get an array of ActiveRecord  
> objects, which is why you are getting an undefined method error.

This returns multiple orders and still works  
@order = Order.find(params[:id])  
is this not an array but something different?

> If you only want to find one record, but pass it conditions, use this:
> 
> @order = Order.find(:first, …)

I don’t necessarily need to pass it a specific condition, just don’t  
want RecordNotFound errors or have to catch that with a rescue  
statement.

There could be more than 1 order, so :first won’t work in this case. I  
have the models for order and orderDetails as:

class Order \< ActiveRecord::Base  
set\_primary\_key “customerId”  
belongs\_to :account  
has\_many :orderDetails, :foreign\_key =\> ‘orderNumber’, :finder\_sql =\>  
'SELECT \* ’ +  
'FROM orderDetails od ’ +  
‘WHERE od.orderNumber = #{orderNumber}’  
end

class OrderDetail \< ActiveRecord::Base  
set\_table\_name “orderDetails”  
belongs\_to :order  
end

so I just need to be able to access the orderDetails part of the order  
object. Guess I just don’t know how to access it.

Thanks for the help!
