Greg Hauptmann
2009-Feb-01 23:19 UTC
how do I use ActiveRecord for selects that return data across multiple tables, or a database view? (ActiveRecord::Base.connection.select gives me a : NoMethodError: private method `select'')
Hi, If I want to return SQL data that cuts across multiple models (e.g. not specific to one mode) how do I do this? Actually the use case could be either (a) custom SQL I want to call or via (b) SQL call to a database view I''ve created? The select method http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M001704 seems to be what I need however when I try to use this I''m getting an error:>> ActiveRecord::Base.connection.select("select * from categories")NoMethodError: private method `select'' called for #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0x253710c> from (irb):11>>-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
James Byrne
2009-Feb-01 23:33 UTC
Re: how do I use ActiveRecord for selects that return data across multiple tables, or a database vie
Greg Hauptmann wrote:> Hi, > > If I want to return SQL data that cuts across multiple models (e.g. > not specific to one mode) how do I do this? Actually the use case > could be either (a) custom SQL I want to call or via (b) SQL call to a > database view I''ve created?If I understand your question then the way to do this is through associations defined in your model. You can have more than one association defined between identical tables, each with a different name and select conditions. See: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html -- 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 -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2009-Feb-02 01:16 UTC
Re: how do I use ActiveRecord for selects that return data across multiple tables, or a database view? (ActiveRecord::Base.connection.select gives me a : NoMethodError: private method `select'')
ok - I see I can do the following ActiveRecord::Base.connection.select_all(''select * from categories'') and get a hash back 2009/2/2 Greg Hauptmann <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Hi, > > If I want to return SQL data that cuts across multiple models (e.g. > not specific to one mode) how do I do this? Actually the use case > could be either (a) custom SQL I want to call or via (b) SQL call to a > database view I''ve created? > > The select method > http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M001704 > seems to be what I need however when I try to use this I''m getting an > error: > >>> ActiveRecord::Base.connection.select("select * from categories") > NoMethodError: private method `select'' called for > #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0x253710c> > from (irb):11 >>> > > > > > -- > Greg > http://blog.gregnet.org/ >-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
James Byrne
2009-Feb-02 02:53 UTC
Re: how do I use ActiveRecord for selects that return data across multiple tables, or a database vi
Greg Hauptmann wrote:> ok - I see I can do the following > > ActiveRecord::Base.connection.select_all(''select * from categories'') > > and get a hash back >How is that superior to: Category.find(:all) Or, if you have conditions: Category.find(:all, :conditions => [:status = ?, my_status]) -- 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 -~----------~----~----~----~------~----~------~--~---