Forum: Ruby on Rails Strange behaviour of dates

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Daniel Salinas (Guest)
on 2006-03-15 09:07
(Received via mailing list)
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:

<label for="app_primary_date_of_birth"><b>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'
date_helper.rb:275:in `to_date_select_tag_without_error_wrapping'
date_helper.rb:274:in `to_date_select_tag_without_error_wrapping'
active_record_helper.rb:170:in `to_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.
Mark Reginald J. (Guest)
on 2006-03-16 02:20
(Received via mailing list)
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.
This topic is locked and can not be replied to.