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’
11155
(-- --)
August 31, 2010, 7:01pm
2
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
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…
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.
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...
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.