unknown wrote:
Hi Wes,
I am a newbie to AJAX style web development and was hoping you
or
someone else could explain what facade columns are and
why/how/where are they used.
Thank you in advance,
Mathew Newton
Mathew,
Basically, any time that you have to customize the look and feel of a
column from the DB, you will have to do something like this.
Here’s the real live example I just figured out (although I’m still
having some issues with the validation :]).
I have a legacy DB schema that contains one field for an email called
“fromLine”.
The fromLine field is formatted as:
“Name of person”
But I want to display this information and allow for data entry with two
fields - one for name and one for email address. So in order to do
that, I need to split up (to read) and merge together (to write) the
name and email address in order to interact correctly with my DB column.
Here’s what you have in the view:
From (name): |
<%= text_field(:current_job, 'from_name') %> |
From (email
address): |
<%= text_field(:current_job, 'from_email') %> |
Here’s what you have to do in the model to make this work - notice the
use of read_attribute and write_attribute - you have to do this because
the form helpers interact with the actual DB attributes, not the custom
ones that you create in the model. The attributes hash persists no
matter what happens. If you have errors, my understanding is that the
object that you get in your view is a brand new object when you
redisplay the page, so it isn’t enough to just reference your custom
attributes (because they would be empty since it’s a new object).
Basically, you have to use the attribute hash to “hold” your custom
stuff. Attributes that aren’t modified just go directly to the
attribute hash.
public
def from_name
line = read_attribute(:fromLine)
if (! line.nil?) && (md = line.match(/"(.*)"/))
md[1]
else
‘’
end
end
public
def from_name=(name)
line = “”#{name}" <#{self.from_email}>"
self.fromLine = line
end
public
def from_email
line = read_attribute(:fromLine)
if (! line.nil?) && (md = line.match(/<(.*)>/))
md[1]
else
‘’
end
end
public
def from_email=(email)
line = “”#{self.from_name}" <#{email}>"
self.fromLine = line
end
public
def fromLine=(line)
write_attribute(:fromLine, line)
end
HTH,
Wes