Take the 2-minute tour ×

I'm using rails 3 app. I have a problem. How will I save data in my session to my database? I want to save the checkin and checkout session to the database with a datetime data type.


def step2
    @cart = current_cart

    checkin = params[:checkin]
    checkout = params[:checkout]
    @amenities = Amenity.available(checkin, checkout)

    session[:checkin] = checkin
    session[:checkout] = checkout


<%= f.hidden_field :Transaction_Date, :value => Time.now %>
    <%= f.hidden_field :rsv_type, :value => "Online" %>
    <%= f.hidden_field :rsv_status, :value => "Pending" %>
    <%= f.hidden_field :checkin, session[:checkin] %>
    <%= f.hidden_field :checkout, session[:checkout] %>
<% end %>

im getting this error:

undefined method `merge' for "05/18/2011":String

help.. please. thanks.

from my applicationcontroller:


def current_cart
rescue ActiveRecord::RecordNotFound
    cart = Cart.create
    session[:cart_id] = cart.id

when i put these code:

@cart.checkin = DateTime.strptime(session[:checkin], "%m/d/%Y %I:%M:%S %p").to_time
@cart.checkout= DateTime.strptime(session[:checkout], "%m/d/%Y %I:%M:%S %p").to_time

i get an error of ArgumentError invalid date. :( what should i do? im using a sql server 2005 for my database.

here's my cart model

class Cart < ActiveRecord::Base has_many :line_items, :dependent => :destroy

def add_amenity(amenity_id)
    current_item = line_items.where(:amenity_id => amenity_id).first
    if current_item
        return false
        current_item = LineItem.new(:amenity_id => amenity_id)
        line_items << current_item

def total_price
    line_items.to_a.sum { |item| item.amenity.fee }

def payable
    total_price * 0.50
share|improve this question

1 Answer 1

You don't need to pass session data in your form (the last two hidden_fields in your example). In general, saving your session data is very simple.

# some controller action with @cart defined
@cart.checkin = session[:checkin]
@cart.checkout = session[:checkout]

Make sure to include any necessary validations on this session data to protect your system and to ensure that data saved to your database is high quality.

Also, session data shouldn't be trusted for critical information, and definitely don't use it to store passwords, financial data related to your shopping cart, etc.

Regarding the date part of your question, this is how to convert a string date to an actual date/time.

DateTime.strptime("2011/05/18 13:42:02", "%Y/%m/%d %H:%M:%S").to_time
share|improve this answer
im getting another error. NoMethodError undefined method `checkin=' for #<Cart:0x3bdbbc8> –  Crisgine May 18 '11 at 1:29
@Crisgine - you have to define @cart properly, such as @cart = Cart.where("id = ?", params[:id]).first. Of course, this assumes your Cart model has a checkin field. –  sscirrus May 18 '11 at 4:18
@Crisgine - bit of background - if you just did @cart = Cart.where("id = ?", params[:id]), you'd get a relation, not a single record. Even if the relation contains only one record, you have to add first to get something you can start changing the attributes of. –  sscirrus May 18 '11 at 5:07
i already have a method for declaring @cart. wait, i will append it to my question and take a look. because, i don't know, how will i use that. :( –  Crisgine May 18 '11 at 5:42
You can do all that in one line :) @cart = Cart.find_or_create_by_id(session[:cart_id]). This should set you up properly. –  sscirrus May 18 '11 at 7:18

Your Answer


