Hi! I''m new in Rails and I''m making my first site using this. I have some problem with methods in models: I have model Note with one additional method: class Note < ActiveRecord::Base belongs_to :pattern def get_location(location) find(:conditions => ["location = ?", location]) end end And when I''m trying to access it through pattern.notes.get_location(1) , it throws an error: undefined method `get_location'' for #<Class:0xb6bc63ec> What i''m doing wrong? cheers! Szymon -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jul-10 16:08 UTC
Re: Additional methods in models - how to access them?
On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I''m new in Rails and I''m making my first site using this. I have some > problem with methods in models: > > I have model Note with one additional method: > > class Note < ActiveRecord::Base > belongs_to :pattern > def get_location(location) > find(:conditions => ["location = ?", location]) > end > end > > And when I''m trying to access it through > pattern.notes.get_location(1) , it throws an error: > > undefined method `get_location'' for #<Class:0xb6bc63ec> >get_location is an instance method of Note but you''re trying to call it on a collection of notes (Because associations are also scopes you can call class methods on them: this just calls the class method with finds etc scoped appropriately, but I''m not sure if that is what you were trying to do). Fred Fred> What i''m doing wrong? > > cheers! > Szymon-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Szymon Przybył
2010-Jul-10 16:21 UTC
Re: Additional methods in models - how to access them?
I''m trying to get Note from this Pattern (which has many notes) with location = 1 for example, how I can do it in rails? simon On 10 Lip, 18:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > I''m new in Rails and I''m making my first site using this. I have some > > problem with methods in models: > > > I have model Note with one additional method: > > > class Note < ActiveRecord::Base > > belongs_to :pattern > > def get_location(location) > > find(:conditions => ["location = ?", location]) > > end > > end > > > And when I''m trying to access it through > > pattern.notes.get_location(1) , it throws an error: > > > undefined method `get_location'' for #<Class:0xb6bc63ec> > > get_location is an instance method of Note but you''re trying to call > it on a collection of notes (Because associations are also scopes you > can call class methods on them: this just calls the class method with > finds etc scoped appropriately, but I''m not sure if that is what you > were trying to do). > > Fred > > Fred > > > What i''m doing wrong? > > > cheers! > > Szymon-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Szymon Przybył
2010-Jul-10 16:48 UTC
Re: Additional methods in models - how to access them?
But even if I call "Note.get_location(1)", it throws the same error :/ What i''m doing wrong? simon On 10 Lip, 18:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > I''m new in Rails and I''m making my first site using this. I have some > > problem with methods in models: > > > I have model Note with one additional method: > > > class Note < ActiveRecord::Base > > belongs_to :pattern > > def get_location(location) > > find(:conditions => ["location = ?", location]) > > end > > end > > > And when I''m trying to access it through > > pattern.notes.get_location(1) , it throws an error: > > > undefined method `get_location'' for #<Class:0xb6bc63ec> > > get_location is an instance method of Note but you''re trying to call > it on a collection of notes (Because associations are also scopes you > can call class methods on them: this just calls the class method with > finds etc scoped appropriately, but I''m not sure if that is what you > were trying to do). > > Fred > > Fred > > > What i''m doing wrong? > > > cheers! > > Szymon-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Leonardo Mateo
2010-Jul-10 17:28 UTC
Re: Re: Additional methods in models - how to access them?
On Sat, Jul 10, 2010 at 1:48 PM, Szymon Przybył <apocalyptiq-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But even if I call "Note.get_location(1)", it throws the same error :/As Frederick says, this is an instance method, you''re trying to call it on the class. You should do something like @pattern.notes.first.get_location I highly recommend you to take a look at the rails guides. http://guides.rubyonrails.org> What i''m doing wrong? > > simon > > On 10 Lip, 18:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> >> > Hi! >> >> > I''m new in Rails and I''m making my first site using this. I have some >> > problem with methods in models: >> >> > I have model Note with one additional method: >> >> > class Note < ActiveRecord::Base >> > belongs_to :pattern >> > def get_location(location) >> > find(:conditions => ["location = ?", location]) >> > end >> > end >> >> > And when I''m trying to access it through >> > pattern.notes.get_location(1) , it throws an error: >> >> > undefined method `get_location'' for #<Class:0xb6bc63ec> >> >> get_location is an instance method of Note but you''re trying to call >> it on a collection of notes (Because associations are also scopes you >> can call class methods on them: this just calls the class method with >> finds etc scoped appropriately, but I''m not sure if that is what you >> were trying to do). >> >> Fred >> >> Fred >> >> > What i''m doing wrong? >> >> > cheers! >> > Szymon > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > >-- Leonardo Mateo. There''s no place like ~ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Szymon Przybył
2010-Jul-10 17:32 UTC
Re: Additional methods in models - how to access them?
This works: pattern.notes.first(:conditions => {:location => location}) But I want use it in views, pattern.notes.get_location(location) looks much better, but how can I make it works? cheers! simon On 10 Lip, 18:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > I''m new in Rails and I''m making my first site using this. I have some > > problem with methods in models: > > > I have model Note with one additional method: > > > class Note < ActiveRecord::Base > > belongs_to :pattern > > def get_location(location) > > find(:conditions => ["location = ?", location]) > > end > > end > > > And when I''m trying to access it through > > pattern.notes.get_location(1) , it throws an error: > > > undefined method `get_location'' for #<Class:0xb6bc63ec> > > get_location is an instance method of Note but you''re trying to call > it on a collection of notes (Because associations are also scopes you > can call class methods on them: this just calls the class method with > finds etc scoped appropriately, but I''m not sure if that is what you > were trying to do). > > Fred > > Fred > > > What i''m doing wrong? > > > cheers! > > Szymon-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Colin Law
2010-Jul-10 20:55 UTC
Re: Re: Additional methods in models - how to access them?
On 10 July 2010 18:32, Szymon Przybył <apocalyptiq-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This works: > > pattern.notes.first(:conditions => {:location => location}) > > But I want use it in views, pattern.notes.get_location(location) looks > much better, but how can I make it works?I am not sure what you are trying to do. Are you trying to find all the notes for that pattern where the location is a specified value? If so you could use Note.find with a condition that note.pattern is the one you want and location is the location you want. In fact I think you should be able to use something like Note.find_all_by_pattern_id_and_location( pattern.id, location ) Alternatively pattern.notes.select{ |n| n.location =required_location } or something similar. If you like this I would make it a method of Pattern so that you can say something like pattern.notes_for_location( required_location ) As an earlier poster suggested, have a look at the rails guides, http://guides.rubyonrails.org By the way it is preferred here to insert replies into the previous post at the appropriate point rather than top posting. It makes it easier to follow the thread. Thanks Colin> > cheers! > simon > > On 10 Lip, 18:08, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On Jul 10, 4:22 pm, Szymon Przybył <apocalyp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> >> > Hi! >> >> > I''m new in Rails and I''m making my first site using this. I have some >> > problem with methods in models: >> >> > I have model Note with one additional method: >> >> > class Note < ActiveRecord::Base >> > belongs_to :pattern >> > def get_location(location) >> > find(:conditions => ["location = ?", location]) >> > end >> > end >> >> > And when I''m trying to access it through >> > pattern.notes.get_location(1) , it throws an error: >> >> > undefined method `get_location'' for #<Class:0xb6bc63ec> >> >> get_location is an instance method of Note but you''re trying to call >> it on a collection of notes (Because associations are also scopes you >> can call class methods on them: this just calls the class method with >> finds etc scoped appropriately, but I''m not sure if that is what you >> were trying to do). >> >> Fred >> >> Fred >> >> > What i''m doing wrong? >> >> > cheers! >> > Szymon > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
class Note < ActiveRecord::Base belongs_to :pattern def get_location(location) find(:conditions => ["location = ?", location]) end end Should be: class Note < ActiveRecord::Base belongs_to :pattern def self.get_location(location) find(:conditions => ["location = ?", location]) end end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
But if you are just doing find(:conditions => ["location = ?", location]) there is already a method defined called find_by_location() example: Note.find_by_location(location) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Szymon Przybył
2010-Jul-11 09:10 UTC
Re: Additional methods in models - how to access them?
Wow, I didn''t know that there are find_by_property methods already defined, ruby and rails are just great! :) I''m switching now from Kohana in PHP to Ruby on Rails, and I can say now, that ruby and rails are much better :) Thanks again! Simon On 11 Lip, 02:49, Kenneth <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> But if you are just doing find(:conditions => ["location = ?", > location]) there is already a method defined called find_by_location() > > example: Note.find_by_location(location) > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.