lucas franceschi
2010-Aug-31 18:48 UTC
How to simulate a column that do not exists in a model
Hello there.. what i am trying to do, is to consider a new column in my model, a column that do not exist in the database, and will not be created. imagine, for instance, this case: I have a table that store a number and a string, lets call it ''words'' -------------------- words -------------------- int times(PK) char name -------------------- imagine Word as an instance of the model, i will have access to these: Word.times Word.name what if i wanted a new column, some like Word.complete imagine that this column would store that ''char'' for many times as is defined int the ''times'' column... like this Word.times = 3 Word.name = a Word.complete = aaa got it? i wont store that data, but generate it based on the data that is stored. any idea???? did any of you guys see this situation before??? -- Lucas Franceschi Desenvolvedor SGI Sistemas (049) 9922-3360 -- 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.
Fritz Trapper
2010-Aug-31 19:01 UTC
Re: How to simulate a column that do not exists in a model
lucas franceschi wrote:> Word.complete = aaaWord is the model class - it doesn''t store values. If word is an instance, simply write: word[:complete] = aaa -- 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.
lucas franceschi
2010-Aug-31 19:06 UTC
Re: Re: How to simulate a column that do not exists in a model
you didn''t get it... the point is.. i want Word.complete to already be defined, so that in the controller i could use the "complete" column as if it was a real column... -- Lucas Franceschi Desenvolvedor SGI Sistemas (049) 9922-3360 On Tue, Aug 31, 2010 at 4:01 PM, Fritz Trapper <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> lucas franceschi wrote: > > Word.complete = aaa > > Word is the model class - it doesn''t store values. > > If word is an instance, simply write: > > word[:complete] = aaa > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Aug-31 19:08 UTC
Re: How to simulate a column that do not exists in a model
On 31 August 2010 19:48, lucas franceschi <lukas1596-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Word.times = 3 > Word.name = a > Word.complete = aaa > got it? > i wont store that data, but generate it based on the data that is stored. > any idea???? > did any of you guys see this situation before???once or twice... what you''re after is a "method"... (you may run into trouble if you try to call your column ''times'', I''ll use ''times_repeated'' in this example) # word.rb def complete self.name * self.times_repeated if self.name end I recommend going through some Ruby tutorials - the "Ruby for Rails" book has a good coverage of getting started through to intermediate use. -- 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.
Michael Pavling
2010-Aug-31 19:11 UTC
Re: How to simulate a column that do not exists in a model
correction - you will need to check that both name and times_repeated are not nil def complete self.name * self.times_repeated if self.name && self.times_repeated end strictly speaking the "self." isn''t needed either, but it does make the intention clearer... -- 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.
Manoj Sachwani
2010-Aug-31 19:31 UTC
Re: How to simulate a column that do not exists in a model
just look up attr_accessor method, if used within a model on a non existant field in the model, it will create a "pseudo" field. Do not forget to make accessible. On Sep 1, 12:11 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> correction - you will need to check that both name and times_repeated > are not nil > > def complete > self.name * self.times_repeated if self.name && self.times_repeated > end > > strictly speaking the "self." isn''t needed either, but it does make > the intention clearer...-- 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.