# Retrieving a record using created\_by

**URL:** <https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485>\
**Category:** Rails\
**Created:** [April 3, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485 "2006-04-03T22:46:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 3, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485/1 "2006-04-03T22:46:27Z")

</div>

Hello,

Thank you all in advance for your help with this.

Environment:

Windows XP Pro (Development only)  
Ruby 1.84  
Rails 1.1.0  
SaltedHashLoginGenerator  
Plugins: file\_column, userstamp

I have a user table per the salted\_hash\_login generator. I have an  
orders table that has a column named created\_by that is updated by the  
userstamp plugin. I am trying to retrieve all of the orders for a single  
user (the one logged in) and am not sure how to do it. When I use this  
code in my order model it works:

def self.my\_orders  
find(:all,  
:conditions =\> “created\_by = 1”,  
:order =\> “created\_on desc”)  
end

I want to create dynamic queries and need to use the created\_by column  
to do it. I appreciate any help you all can give me. Also, in order to  
not ask so many newbie questions, I have purchased a few books: Agile  
Web D. with Rails, Rails Recipes, and the O’Reilly (beta) book  
Rough Cuts: Ruby on Rails - Up and Running. Can anyone point me in the  
direction of other good books? Thanks.

Sincerely,

Robert D.

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 4, 2006, 7:38am UTC](https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485/2 "2006-04-04T07:38:59Z")

</div>

On Apr 3, 2006, at 22:46, Robert D. wrote:

> def self.my\_orders  
> find(:all,  
> :conditions =\> “created\_by = 1”,  
> :order =\> “created\_on desc”)  
> end
> 
> I want to create dynamic queries and need to use the created\_by column  
> to do it.

orders = Order.find\_by\_created\_by(@user.id)

Or even better if you set up assocations:

class User \< ActiveRecord::Base  
has\_many :orders, :foreign\_key =\> ‘created\_by’, :order =\>  
‘created\_on desc’  
end

orders = @user.orders

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 4, 2006, 7:56am UTC](https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485/3 "2006-04-04T07:56:09Z")

</div>

Robert D. wrote:

> Can anyone point me in the  
> direction of other good books? Thanks.

I strongly recommend Ruby for Rails by David Black (  
[Ruby for Rails](http://manning.com/books/black) ). I’ve had many “aha” moments so far and  
still got 6 chapters to go. It’s very well written and easy to  
understand.

/Stefan

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [April 4, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/retrieving-a-record-using-created-by/93485/4 "2006-04-04T12:20:12Z")

</div>

Stefan Liden wrote:

> Robert D. wrote:
> 
> > Can anyone point me in the  
> > direction of other good books? Thanks.
> 
> I strongly recommend Ruby for Rails by David Black (  
> [Ruby for Rails](http://manning.com/books/black) ). I’ve had many “aha” moments so far and  
> still got 6 chapters to go. It’s very well written and easy to  
> understand.
> 
> /Stefan

Thank you both for your help. I will implement the given code and  
purchase the book right away. I hope to cut down on all of my newbie  
questions. Thank you again.

Sincerely,

Robert D.
