Hi! I''m new to the ruby on rails and have a question about find_by_sql method. I have two model A and B and I want to select all records from B with the A model. The following code: A.find_by_sql("select * from B") returns nothing. Are there any methods to do it? Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 5 Jun 2008, at 16:42, psyche wrote:> > Hi! > > I''m new to the ruby on rails and have a question about find_by_sql > method. I have two model A and B and I want to select all records from > B with the A model. > The following code: A.find_by_sql("select * from B") returns nothing.Foo.find_by_sql returns instances of Foo. What are you trying to do ? Fred> > Are there any methods to do it? > > Thanks. > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
oops thats very un-rails ;-) (but should work) first: are those tables related? Or why do you want to use A to retrieve B??? however why don''t you use B.find(:all) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for your comments. Actually it was a bad example :) I have a User and Role models with has_and_belongs_to_many relationship. I want to create named scope for admin users. Something like this: named_scope :admin, :joins => "LEFT JOIN roles_users ON roles_users.user_id=users.id", :conditions => { :role_id => Role.find(:first, :conditions => { :name => ''admin'' })} But Rails returns the following sql: SELECT * FROM `users` LEFT JOIN roles_users ON roles_users.user_id=users.id WHERE (`users`.`role_id` = 1) The bad thing: `users`.`role_id`. Are there any method to get `role_id` only? On Jun 5, 7:51 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> oops thats very un-rails ;-) > (but should work) > > first: are those tables related? Or why do you want to use A to retrieve > B??? > > however why don''t you use > > B.find(:all) > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I do not know is it a hack or a solution: User.scoped :joins => "LEFT JOIN roles_users ON roles_users.user_id=users.id WHERE role_id {Role.find(:first, :conditions => { :name => "admin" }, :select => :id).id}" Hope it helps somebody. With "Role.find(:first..." I have two queries, but if admin role_id would determined in the spec it would be only one query. On Jun 5, 8:06 pm, psyche <timofeev.kir...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for your comments. Actually it was a bad example :) > > I have a User and Role models with has_and_belongs_to_many > relationship. I want to create named scope for admin users. > > Something like this: > named_scope :admin, :joins => "LEFT JOIN roles_users ON > roles_users.user_id=users.id", :conditions => { :role_id => > Role.find(:first, :conditions => { :name => ''admin'' })} > > But Rails returns the following sql: SELECT * FROM `users` LEFT JOIN > roles_users ON roles_users.user_id=users.id WHERE (`users`.`role_id` > = 1) > > The bad thing: `users`.`role_id`. Are there any method to get > `role_id` only? > > On Jun 5, 7:51 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > oops thats very un-rails ;-) > > (but should work) > > > first: are those tables related? Or why do you want to use A to retrieve > > B??? > > > however why don''t you use > > > B.find(:all) > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Using named_scope it would only one query (truthly: only one additional query on startup). Cool! :) 2008/6/5 psyche <timofeev.kirill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > I do not know is it a hack or a solution: > User.scoped :joins => "LEFT JOIN roles_users ON > roles_users.user_id=users.id WHERE role_id > {Role.find(:first, :conditions => { :name => "admin" }, :select > => :id).id}" > > Hope it helps somebody. > > With "Role.find(:first..." I have two queries, but if admin role_id > would determined in the spec it would be only one query. > > On Jun 5, 8:06 pm, psyche <timofeev.kir...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Thanks for your comments. Actually it was a bad example :) >> >> I have a User and Role models with has_and_belongs_to_many >> relationship. I want to create named scope for admin users. >> >> Something like this: >> named_scope :admin, :joins => "LEFT JOIN roles_users ON >> roles_users.user_id=users.id", :conditions => { :role_id => >> Role.find(:first, :conditions => { :name => ''admin'' })} >> >> But Rails returns the following sql: SELECT * FROM `users` LEFT JOIN >> roles_users ON roles_users.user_id=users.id WHERE (`users`.`role_id` >> = 1) >> >> The bad thing: `users`.`role_id`. Are there any method to get >> `role_id` only? >> >> On Jun 5, 7:51 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >> >> > oops thats very un-rails ;-) >> > (but should work) >> >> > first: are those tables related? Or why do you want to use A to retrieve >> > B??? >> >> > however why don''t you use >> >> > B.find(:all) >> > -- >> > Posted viahttp://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ActiveRecord::Base.connection.select_all("select * from users") returns all rows. 2008/6/5 Kirill Timofeev <timofeev.kirill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Using named_scope it would only one query (truthly: only one > additional query on startup). Cool! :) > > 2008/6/5 psyche <timofeev.kirill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> >> I do not know is it a hack or a solution: >> User.scoped :joins => "LEFT JOIN roles_users ON >> roles_users.user_id=users.id WHERE role_id >> {Role.find(:first, :conditions => { :name => "admin" }, :select >> => :id).id}" >> >> Hope it helps somebody. >> >> With "Role.find(:first..." I have two queries, but if admin role_id >> would determined in the spec it would be only one query. >> >> On Jun 5, 8:06 pm, psyche <timofeev.kir...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Thanks for your comments. Actually it was a bad example :) >>> >>> I have a User and Role models with has_and_belongs_to_many >>> relationship. I want to create named scope for admin users. >>> >>> Something like this: >>> named_scope :admin, :joins => "LEFT JOIN roles_users ON >>> roles_users.user_id=users.id", :conditions => { :role_id => >>> Role.find(:first, :conditions => { :name => ''admin'' })} >>> >>> But Rails returns the following sql: SELECT * FROM `users` LEFT JOIN >>> roles_users ON roles_users.user_id=users.id WHERE (`users`.`role_id` >>> = 1) >>> >>> The bad thing: `users`.`role_id`. Are there any method to get >>> `role_id` only? >>> >>> On Jun 5, 7:51 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >>> wrote: >>> >>> > oops thats very un-rails ;-) >>> > (but should work) >>> >>> > first: are those tables related? Or why do you want to use A to retrieve >>> > B??? >>> >>> > however why don''t you use >>> >>> > B.find(:all) >>> > -- >>> > Posted viahttp://www.ruby-forum.com/. >> >> >> >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---