Link to home
Start Free TrialLog in
Avatar of rlbertke
rlbertke

asked on

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

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
    SessionLog.find(session[:session_log_id])

    rescue ActiveRecord::RecordNotFound

    session_log = SessionLog.create
    session[:session_log_id] = session_log.id
    session_log

end

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 = SessionLog.new) 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


Avatar of kristinalim
kristinalim
Flag of Philippines image

Using .new instead of .create would not attempt to save the record in the database (not what you want, I presume), so session_log.id 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.
Avatar of rlbertke
rlbertke

ASKER

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 = DateTime.now
  end
ASKER CERTIFIED SOLUTION
Avatar of kristinalim
kristinalim
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correction: The more common way of achieving your goal would be with ActiveRecord::Base after_initialize().
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!