Retrieving a record using created_by

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.

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

Robert D. wrote:

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

I strongly recommend Ruby for Rails by David Black (
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

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 (
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs