Error when using either .create or .new with Rails 3

Posted on 2011-03-08
Last Modified: 2013-11-13
I have an application that worked in rails 2.3.8, but now trying to convert to rails 3 and things aren't working like I would expect.

I have a model (and table) called 'session_log' (model is SessionLog).  This table contains information about the current user (logged in status, User ID etc.).

In additon, I used the command rake:db:sessions:create which created the table 'sessions'.  I'm not sure I understand the implications of this completely but don't think that is impacting what I'm dealing with.

The issue is that in the application_controller.rb file, I have a method defined as follows:

def get_session

    rescue ActiveRecord::RecordNotFound

    session_log = SessionLog.create
    session[:session_log_id] =


This code is mostly copied from the agile web development book (for rails 3).

the code in the controller that calls this method is as follows:

@current_session = get_session

When I run this code, I get an error 'wrong number of arguments (1 for 0)' and I can't figure out why this would be the case.
The get_session method does not expect (nor do I pass) an argument.  the only thing I could think of is that the .create method was
expecting an argument, although I've read code examples that suggest this isn't the case.

I tried using .new (session_log = instead, and get a different error 'You have a nil object when you didn't expect it' and 'you might have an instance of ActiveRecord::Base'.

Any help in pointing me in the right direction would be appreciated

Question by:rlbertke
  • 3
  • 2

Expert Comment

ID: 35070158
Using .new instead of .create would not attempt to save the record in the database (not what you want, I presume), so and session[:session_log_id] are expected to be nil. Maybe you were calling an ActiveRecord::Base method on the get_session result or session[:session_log_id] elsewhere causing the "You have a nil object when you didn't expect it" error?

Anyway, your get_session method looks fine. Will you be able to give us your code in the area where the error occurs? Look for the line number. If you added another rescue elsewhere and only print out the more readable form of the error, it might be useful to remove this temporarily so you could see the line number.

Author Comment

ID: 35075156
I just noticed something interesting.  The code for the model has an initialize method in it.  When i comment that out, I don't get an error (it works) but if I include the method, even with all the code inside commented out, it still doesn't work.  Any reason why this would be the case?

Below is the initialize method in the class SessionLog

  def initialize
#    @logged_in = "False"
#    @created_at =

Accepted Solution

kristinalim earned 500 total points
ID: 35076451
Is logged_in really meant to be a string or is that a typo?

You are overriding the ActiveRecord::Base initialize(attributes = nil) method of ActiveRecord::Base which allows one parameter with a method that does not allow a parameter. There are various places throughout ActiveRecord::Base that call this with a parameter, and ActiveRecord::Base create(...) is only one of these.

The more common way of achieving your goal would be with ActiveRecord::Base initialize():

def after_initialize
  @logged_in = false
  @created_at =

Open in new window

Slightly unrelated concerns especially if any of these attributes are non-virtual:

Line 3: By default, a :created_at non-virtual attribute will automatically be set to the date and time the record is saved. In most circumstances, there is no need to set it yourself.

Line 2: :logged_in will be set to false in the following scenarios as well. Consider setting the default for :logged_in in the DB instead, or setting only if an unsaved record (new_record?) and if not changed (!logged_in_changed?). => true)

Open in new window


Expert Comment

ID: 35076516
Correction: The more common way of achieving your goal would be with ActiveRecord::Base after_initialize().

Author Closing Comment

ID: 35082409
This helped immensly.  The 'logged_in' sadly was created as a string.  This was my first RoR project and when I first created these attributes I didn't want to get bogged down with data types.  This project is 99% web site with 1% database/user interaction and I wasn't concerned with getting things right as much as getting them working.

The app worked fine in RoR 2.3.8, but (with the initialize ovverride) but porting to Rails 3, it stopped liking this.  

I changed to use the after_initialize method and it works great and I get the fact that I'm overriding a method that previously expected a parameter.

I also appreciate the additional feedback on using the model to set the default values.  Thank you!

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unable to install Rails Gem 6 1,418
return captures in array 5 252
RVM setup 5 189
Ruby - Extract Year from document text 6 84
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now