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 Vit
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]
Fredrik1
(Fredrik)
4
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