How to avoid SQL query?

This is only example.
I’ve got two tables users and posts.

[1]
select * from users, posts where [ posts conditions ] and users.id =
posts.user_id order by posts.title desc

Post.find(:all, :conditions => […], :order => “title DESC”)

[2]
select * from users, posts where [ posts conditions ] and users.id =
posts.user_id order by users.name

Post.find … ???

How to avoid SQL query using Active Record find methods??

Hi Daniel,

Your example is a bit vague to give a full answer… Maybe this will
help:
http://www.therailsway.com/2007/3/26/association-proxies-are-your-friend

–Andrew V.

hello,
you can use find_by_sql :

Post.find_by_sql[“select * from users, posts where posts.id =? and
users.id = posts.user_id order by users.name” , post_id]

Post.find(:all, :include => :user, :conditions => [“posts.id = ? AND
users.id = ?”,postid,userid], :order => “users.name”)

class Post < ActiveRecord::Base
belongs_to :user
end

class User < ActiveRecord::Base
has_many :posts
end

Fredrik

On Apr 30, 11:58 am, Jean-Sébastien [email protected]

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