CoolAJ86
2009-Aug-23 21:41 UTC
Can non-ActiveRecord variables be temporarily stored in a class?
I''m creating a PDF directory using PDF::Writer. The directory has a few pieces. * The photo directory is sorted by group * The appendix is sorted by last name I want the appendix to show the page number on which the person is printed, but I don''t want to store that in the database. I would like to do this: contact.page_number = page_number contacts << contact but currently I''m doing this tuple[:contact => contact, :page_number => page_number] contacts << tuple
CoolAJ86
2009-Aug-25 17:38 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
> > The directory has a few pieces. > > * The photo directory is sorted by group > > * The appendix is sorted by last name > > > I want the appendix to show the page number on which the person is > > printed, but I don''t want to store that in the database. > > An activerecord object is a normal ruby object - it can have non > database store instance variables if you want.but when I do this: contact.page_number = page_number where page_number doesn''t exist in db/migrate/001_create_contacts.rb or app/model/contacts.rb I get this: undefined method `page_number='' for #<Contact:0xb6bbb5b4>
Frederick Cheung
2009-Aug-25 17:47 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
On Aug 25, 6:38 pm, CoolAJ86 <coola...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > The directory has a few pieces. > > > * The photo directory is sorted by group > > > * The appendix is sorted by last name > > > > I want the appendix to show the page number on which the person is > > > printed, but I don''t want to store that in the database. > > > An activerecord object is a normal ruby object - it can have non > > database store instance variables if you want. > > but when I do this: > contact.page_number = page_numberWell yes - you still need to write the accessor methods to store you instance variables (attr_accessor is probably enough). Fred> where page_number doesn''t exist in db/migrate/001_create_contacts.rb > or app/model/contacts.rb > I get this: > undefined method `page_number='' for #<Contact:0xb6bbb5b4>
CoolAJ86
2009-Aug-30 07:36 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
> Well yes - you still need to write the accessor methods to store you > instance variables (attr_accessor is probably enough). > > FredThat makes sense. Thanks. I had my JavaScript thinking cap on when I was doing this... thinking to create accessors out of thin air. I also just found out that the virtual accessors are accessors, not class variables. return @updated_at # always null return updated_at # works
Colin Law
2009-Aug-30 08:17 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
2009/8/30 CoolAJ86 <coolaj86-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> >> Well yes - you still need to write the accessor methods to store you >> instance variables (attr_accessor is probably enough). >> >> Fred > > That makes sense. Thanks. > > I had my JavaScript thinking cap on when I was doing this... thinking > to create accessors out of thin air. > > I also just found out that the virtual accessors are accessors, not > class variables. > return @updated_at # always null > return updated_at # worksDon''t understand this. As I understand it if you have attr_accessor :my_var then @my_var will access the variable (but this may only be written inside the class) and my_var is a method (well two methods actually) that may be used externally to read/write to @my_var so you can say my_object.my_var = 1 x = my_object.my_var If you use my_var (no @) inside the class this should work but it is calling the accessor methods rather than directly accessing @my_var Colin
Colin Law
2009-Aug-30 11:44 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
2009/8/30 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > On 30 Aug 2009, at 09:17, Colin Law wrote: > >> >> 2009/8/30 CoolAJ86 <coolaj86-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >>> >>>> Well yes - you still need to write the accessor methods to store you >>>> instance variables (attr_accessor is probably enough). >>>> >>>> Fred >>> >>> That makes sense. Thanks. >>> >>> I had my JavaScript thinking cap on when I was doing this... thinking >>> to create accessors out of thin air. >>> >>> I also just found out that the virtual accessors are accessors, not >>> class variables. >>> return @updated_at # always null >>> return updated_at # works >> >> Don''t understand this. > > I think what the previous poster has realised is that activerecord > attributes are not stored inside individual instance variables.Ah, yes Colin> > Fred >> As I understand it if you have >> attr_accessor :my_var >> then @my_var will access the variable (but this may only be written >> inside the class) >> and my_var is a method (well two methods actually) that may be used >> externally to read/write to @my_var so you can say >> my_object.my_var = 1 >> x = my_object.my_var >> >> If you use my_var (no @) inside the class this should work but it is >> calling the accessor methods rather than directly accessing @my_var >> >> Colin >> >> > > > > > >
alexander sviridoff
2009-Aug-30 13:22 UTC
Re: Can non-ActiveRecord variables be temporarily stored in a class?
Maybe this is useful http://forums.site5.com/showthread.php?t=18522 On 30 авг, 06:44, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/8/30 Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > On 30 Aug 2009, at 09:17, Colin Law wrote: > > >> 2009/8/30 CoolAJ86 <coola...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > >>>> Well yes - you still need to write the accessor methods to store you > >>>> instance variables (attr_accessor is probably enough). > > >>>> Fred > > >>> That makes sense. Thanks. > > >>> I had my JavaScript thinking cap on when I was doing this... thinking > >>> to create accessors out of thin air. > > >>> I also just found out that the virtual accessors are accessors, not > >>> class variables. > >>> return @updated_at # always null > >>> return updated_at # works > > >> Don''t understand this. > > > I think what the previous poster has realised is that activerecord > > attributes are not stored inside individual instance variables. > > Ah, yes > > Colin > > > > > Fred > >> As I understand it if you have > >> attr_accessor :my_var > >> then @my_var will access the variable (but this may only be written > >> inside the class) > >> and my_var is a method (well two methods actually) that may be used > >> externally to read/write to @my_var so you can say > >> my_object.my_var = 1 > >> x = my_object.my_var > > >> If you use my_var (no @) inside the class this should work but it is > >> calling the accessor methods rather than directly accessing @my_var > > >> Colin