Forum: Ruby on Rails Retrieving a record using created_by

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.
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-03 22:46
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 Development 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 Dempsey
317aad7f9f0b69a440faca74bbe22b20?d=identicon&s=25 Jakob Skjerning (Guest)
on 2006-04-04 07:38
(Received via mailing list)
On Apr 3, 2006, at 22:46, Robert Dempsey 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 Skjerning - http://mentalized.net
50b0c7ff7b5436d6585e8384045cc04a?d=identicon&s=25 Stefan Liden (Guest)
on 2006-04-04 07:56
Robert Dempsey 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
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-04 12:20
Stefan Liden wrote:
> Robert Dempsey 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 Dempsey
This topic is locked and can not be replied to.