Strange behaviour of dates

Imagine a crud app that tracks people applying for a loan, here is the
layout I have:

Mysql database:
create table apps (
id int auto_increment primary key,
primary_id int not null,
secondary_id int null,
constraint fk_primary_id foreign key(primary_id) references
constraint fk_secondary_id foreign key(secondary_id) references

create table contacts (
Id int auto_increment primary key,
Name char not null,
Date_of_birth date not null)

class App < ActiveRecord::Base
belongs_to :primary,
:class_name => “Contact”,
:foreign_key => “primary_id”
belongs_to :secondary,
:class_name => “Contact”,
:foreign_key => “secondary_id”

class Contact < ActiveRecord::Base
has_many :apps_issued,
:class_name => “App”,
:foreign_key => “primary_id”
has_many :apps,
:class_name => “App”,
:foreign_key => “secondary_id”

I have controllers to edit contacts individually and as a whole
When I look at the controller Contact from a browser and select a
contact to
edit I get the proper form up on screen and this section:

  <label for="contact_date_of_birth"><b>Date of birth: 

<%= date_select ‘contact’, ‘date_of_birth’, :start_year => 1900 %>

Returns 3 drop downs that are auto populated with the contacts dob in

Now when I go to the /App controller and get my list, I have a custom
the basically looks like this:

def edit
@app = App.find(params[:id],
:joins => “LEFT OUTER JOIN contacts ON LEFT OUTER JOIN contacts AS secondary ON
:select => “, as primary_name,
contacts.date_of_birth as primary_date_of_birth, as
secondary_name, secondary.date_of_birth as secondary_date_of_birth”)

This line in my _edit.rhtml:

Date of birth:

<%= date_select ‘app’, ‘primary_date_of_birth’, :start_year => 1900 %>


Showing app/views/app/_edit.rhtml where line #36 raised:

undefined method `year’ for “1993-02-05”:String

date_helper.rb:222:in select_year' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/ date_helper.rb:275:into_date_select_tag_without_error_wrapping’
date_helper.rb:274:in to_date_select_tag_without_error_wrapping' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/ active_record_helper.rb:170:into_date_select_tag’
date_helper.rb:80:in `date_select’

So I guess the question of the century is what is done to the date of
when the Contact controller accesses it with the default find(:all) that
not being done to it when I do my custom find that results in the error?

Any and all help will be greatly appreciated.

Daniel Salinas wrote:

secondary_name, secondary.date_of_birth as secondary_date_of_birth")
Showing app/views/app/_edit.rhtml where line #36 raised:

undefined method `year’ for “1993-02-05”:String

Extra-table attributes that are tacked onto a model instance using a
custom select, as well plain attr_accessor attributes, aren’t typed
column objects which can be used with multi-parameter AR helpers.

Instead use:

<%= select_date @app.primary_date_of_birth,
:prefix => ‘app[primary_date_of_birth]’,
:start_year => 1900 %>

And in the app model:

def primary_date_of_birth
Date.parse( self[primary_date_of_birth] )

def primary_date_of_birth= (d)
self[:primary_date_of_birth] =[:year],d[:month],d[:day])

We develop, watch us RoR, in numbers too big to ignore.