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?? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Apr 29, 5:51 am, Daniel Para <daniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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??--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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] On Apr 29, 2:51 pm, Daniel Para <daniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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??--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <jeansebastien....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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] > > On Apr 29, 2:51 pm, Daniel Para <daniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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??--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---