Forum: Ruby on Rails Strange behaviour of dates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
(contacts.id),
constraint fk_secondary_id foreign key(secondary_id) references
(contacts.id))

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


Models
app.rb
class App < ActiveRecord::Base
   belongs_to :primary,
    :class_name => "Contact",
    :foreign_key => "primary_id"
  belongs_to :secondary,
    :class_name => "Contact",
    :foreign_key => "secondary_id"
end

contact.rb
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"
end

I have controllers to edit contacts individually and as a whole
application.
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:
</b></label><br>

<%= date_select 'contact', 'date_of_birth', :start_year => 1900 %>

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

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

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

This line in my _edit.rhtml:

<label for="app_primary_date_of_birth"><b>Date of birth:
</b></label><br>

<%= date_select 'app', 'primary_date_of_birth', :start_year => 1900 %>

returns:

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

undefined method `year' for "1993-02-05":String


/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/
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:in `to_date_select_tag_without_error_wrapping'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/
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:in `to_date_select_tag'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/
date_helper.rb:80:in `date_select'
#{RAILS_ROOT}/app/views/app/_edit.rhtml:36




So I guess the question of the century is what is done to the date of
birth
when the Contact controller accesses it with the default find(:all) that
is
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] )
end

def primary_date_of_birth= (d)
   self[:primary_date_of_birth] = Date.new(d[:year],d[:month],d[:day])
end


--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.