# Problem with date object

**URL:** <https://www.ruby-forum.com/t/problem-with-date-object/169922>\
**Category:** Rails\
**Created:** [June 10, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922 "2009-06-10T09:39:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 10, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/1 "2009-06-10T09:39:03Z")

</div>

Hello All,

My problem is i have field “expiry\_date” which is a “date” type.  
I want to take that date and store it in database as following

## in controller

def create

```
  cc_information = params[:credit_card]
  @credit_card = CreditCard.new
  @credit_card.first_name = cc_information['first_name']
  @credit_card.last_name = cc_information['last_name']
  @credit_card.cc_number = cc_information['cc_number']
  .........
  .........

  @credit_card.expiry_date =

```

Date.civil(cc\_information[‘expiry\_date(1i)’],  
cc\_information[‘expiry\_date(2i)’], cc\_information[‘expiry\_date(3i)’])

end

I can use this also @credit\_card = CreditCard.new(params[:credit\_card])

But I don’t want it. I want to store “expiry\_date” using Date.civil or  
Date.civil\_to\_jd

## but it throws me error

ArgumentError in Credit cardsController#create

comparison of String with 0 failed

RAILS\_ROOT: D:/subscription\_manegement  
Application Trace | Framework Trace | Full Trace

## c:/ruby/lib/ruby/1.8/date.rb:596:in `<' c:/ruby/lib/ruby/1.8/date.rb:596:in`valid\_civil?’ c:/ruby/lib/ruby/1.8/date.rb:726:in `civil' D:/subscription_manegement/app/controllers/credit_cards_controller.rb:52:in`create’

## In view new.html.erb

\<% form\_for :credit\_card, @credit\_card, :url =\> credit\_cards\_path, :html  
=\>{:method =\> :post} do |f| %\>  
\<%= f.error\_messages %\>

…  
… other fields

\<%= f.label :expiry\_date %\>  
 \<%= f.date\_select :expiry\_date %\>

please tell me Where I am doing wrong?

Thanks,  
Vikas

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 10, 2009, 9:44am UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/2 "2009-06-10T09:44:48Z")

</div>

On Jun 10, 8:39 am, Vikas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I can use this also @credit\_card = CreditCard.new(params[:credit\_card])
> 
> But I don’t want it. I want to store “expiry\_date” using Date.civil or  
> Date.civil\_to\_jd

why not let activerecord do the hard work for you ? Anywway, the  
problem here is that you need to turn those strings into integers  
before giving them to Date.civil.

Fred

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 10, 2009, 11:18am UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/3 "2009-06-10T11:18:34Z")

</div>

Let active record do the work for you. Put in a  
before\_validation\_on\_create (or update or whatever) hook in your model  
and do the changes you want. Also, isn’t Date.civil the same as the  
normal date convention we use?

On Jun 10, 12:39 pm, Vikas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 12, 2009, 11:32am UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/4 "2009-06-12T11:32:57Z")

</div>

Frederick C. wrote:

> On Jun 10, 8:39ï¿½am, Vikas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I can use this also @credit\_card = CreditCard.new(params[:credit\_card])
> > 
> > But I don’t want it. I want to store “expiry\_date” using Date.civil or  
> > Date.civil\_to\_jd
> 
> why not let activerecord do the hard work for you ? Anywway, the  
> problem here is that you need to turn those strings into integers  
> before giving them to Date.civil.
> 
> Fred

Thanks Fred, i didn’t use to\_i method to convert string to integer.

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 12, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/5 "2009-06-12T15:22:47Z")

</div>

Ryan B. wrote:

> Just FYI:
> 
> It is illegal to store credit card information from your users  
> (without proper clearance from certain authorities). Check out the PCI  
> standard.

That is a recommendation. I don’t think it has the force of law – at  
least in the US – but do check before you do anything stupid!

> I recommend using a payment gateway like PaymentExpress.

Indeed.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 12, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/problem-with-date-object/169922/6 "2009-06-12T14:24:49Z")

</div>

Just FYI:

It is illegal to store credit card information from your users  
(without proper clearance from certain authorities). Check out the PCI  
standard.

I recommend using a payment gateway like PaymentExpress.
