Hello, this is a repost, something seemed to go wrong last time I tried to post... Anyway I got this model: class Command < ActiveRecord::Base has_many :executions def with_extension(ext) Extension.all(:include => :command, :conditions => { :suffix => ext}) end end And this code in a controller method @commands = Command.with_extension(''txt'') But I get this error then undefined method `with_extension'' for #<Class:0xb6eaecbc> I''m obviously doing something wrong, but since I''m not very experienced with ruby, I don''t know what it is... Another problem I have is that I''m not sure the method code is right. This is the extensions schema: create_table "extensions", :id => false, :force => true do |t| t.string "suffix", :null => false t.integer "command_id", :null => false end I want to select all commands which is mapped to a specific suffix. Niklas Ulvinge thanks you for your time and wishes you and everybody Happy programming! -- 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 -~----------~----~----~----~------~----~------~--~---
On Aug 23, 5:47 am, Niklas Ulvinge <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, this is a repost, something seemed to go wrong last time I tried > to post... > > Anyway I got this model: > class Command < ActiveRecord::Base > has_many :executions > > def with_extension(ext) > Extension.all(:include => :command, > :conditions => { :suffix => ext}) > endThis is a method on an instance,> end > > And this code in a controller method > @commands = Command.with_extension(''txt'')But here it is being called for the class. try: def self.with_extension ... end in the Command class. However, since the with_extension method only really interacts with the Extension class I think that would be a better place for it. class Extension < ActiveRecord::Base def self.all_with_extension(ext) Extension.all(...) # code to find all required extensions end end Allan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Allan wrote:> This is a method on an instance,Hmm, ok.>> >> And this code in a controller method >> @commands = Command.with_extension(''txt'') > > try: > > def self.with_extension > ... > endI did, but I still got the same error...> > However, since the with_extension method only really interacts with > the Extension class I think that would be a better place for it. >My second question was how to perform a complex query so it returned all commands which have a command:suffix mapping. I could do it with code something like this, but it it awfully inefficient: Extension.all(:include => :command, :conditions => { :suffix => ext}).map { |e| e.command } I don''t think that belongs in the extension class. -- 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 -~----------~----~----~----~------~----~------~--~---