Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:26:45+00:00 2026-05-30T08:26:45+00:00

I have read a few tutorials and in a book about using Active Record,

  • 0

I have read a few tutorials and in a book about using Active Record, but I am having trouble tying the pieces together. I used devise to create a model called User:

class User < ActiveRecord::Base
  has_many :tasks
  ....
end

I used scaffold to create Task:

class Task < ActiveRecord::Base
  belongs_to :user#, :autosave => true

  validates_presence_of :description, :date
  validates_numericality_of :hours

  def add_user(creating_user)
    user << creating_user
  end
end

task_controller.rb

# POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(params[:task])

    # add in user to task
    @task.add_user current_user

    respond_to do |format|
      if @task.save
       .... #default code
      end
    end
  end

When I try to save a task then through the view it complains about the object being nil:

NoMethodError in TasksController#create

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<
Rails.root: /home/steven/csc309/project/Source/TPHNN

Application Trace | Framework Trace | Full Trace
app/models/task.rb:8:in `add_user'
app/controllers/tasks_controller.rb:48:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"zSslFLGZ+dbwr1+TBlUtq1F89chUtlBSV3qTHe6G0yY=",
 "task"=>{"id"=>"-4",
 "hours"=>"1",
 "miles"=>"2",
 "description"=>"dd",
 "date(1i)"=>"2012",
 "date(2i)"=>"2",
 "date(3i)"=>"23",
 "date(4i)"=>"23",
 "date(5i)"=>"34"},
 "commit"=>"Create Task"}
Show session dump

_csrf_token: "zSslFLGZ+dbwr1+TBlUtq1F89chUtlBSV3qTHe6G0yY="
session_id: "31e12032237fb42146b5291ab01cf71f"
warden.user.user.key: ["User", [1], "$2a$10$9P8RUZvg6r4LNSRy5FOdFu"]

Thanks for the help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:26:47+00:00Added an answer on May 30, 2026 at 8:26 am

    Close but not quite there! This is probably more what you want:

    class Task < ActiveRecord::Base
      belongs_to :user#, :autosave => true
    
      validates_presence_of :description, :date
      validates_numericality_of :hours
    
      def add_user(creating_user)
        user = creating_user
      end
    end
    

    Or you could simplify matters in your controller and do it all in one step there:

    def create
      @task = current_user.tasks.new(params[:task])
    
      ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning about Pylons and I've read a few tutorials, but none of them
I have read a few tutorials and did some search but I must be
I'm fairly new to working with relational databases, but have read a few books
I have read quite a few selcet+update questions in here but cannot understand how
I am new to serial communication. I have read a fair few tutorials, and
I have read several tutorials, but I just can't this to work. My apache
I'm doing a simple GUI using wxRuby. I have just only read the tutorials
I have read a few tutorials ( Catalin Zima , TheCansin ) regarding Deferred
I have recently started looking into using Azure but I'm having some issues getting
I have read few F# tutorials and I have noticed how easy is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.