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 6843787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:17:39+00:00 2026-05-27T00:17:39+00:00

I am working on an app where users’ projects can follow plant objects from

  • 0

I am working on an app where users’ “projects” can follow “plant” objects from the database. I am getting the following error for the create action in my “Prelationships” controller (Plant Relationships that connect Users’ projects to fixed “plant” objects) when I hit “Follow” for any number of plant objects in my app:

“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.[]”

I realize this is a big question, and yes, I’m pretty much a newbie. All the migrations should be fine. I appreciate any help–even if it means suggesting a whole new way of tackling this issue.

Here’s what my controller, called “Prelationships”, looks like:

class PrelationshipsController < ApplicationController  
 def create                                                    
  @plant = Plant.find(params[:prelationship][:pfollowed_id])
  @project.follow!(@plant)
  respond_to do |format|
    format.html { redirect_to @project }
    format.js
  end
 end  
end        

And my “Prelationships” model:

class Prelationship < ActiveRecord::Base                          
 attr_accessible :pfollowed_id

   belongs_to :pfollower, :class_name => "Project"
   belongs_to :pfollowed, :class_name => "Plant"

   validates :pfollower_id, :presence => true
   validates :pfollowed_id, :presence => true
end

And my “Projects” model:

class Project < ActiveRecord::Base                    
attr_accessible :title, :address, :latitude, :longitude, :state      

 belongs_to :user        
 has_many :prelationships, :foreign_key => "pfollower_id",
                        :dependent => :destroy   
 has_many :pfollowing, :through => :prelationships, :source => :pfollowed  

 def pfollowing?(pfollowed)
   prelationships.find_by_pfollowed_id(pfollowed)  
 end  

 def pfollow!(pfollowed)
  prelationships.create!(:pfollowed_id => pfollowed.id)
 end
end

And my “plant” model:

class Plant < ActiveRecord::Base      

 has_many :prelationships, :foreign_key => "pfollowed_id",
                        :class_name => "Prelationship"
 has_many :pfollowers, :through => :reverse_prelationships, 
                        :source => :pfollower   
end

And, finally, my “_plants_form” partial for the view:

<%= form_for @project.prelationships.build(:pfollowed_id =>
                                           @project_id) do |f| %>
  <%= collection_select(:prelationships, :pfollowed_id, Plant.all, :id, :name, 
  options = {:prompt => "Select your plants"}, :class => "listselect") %>  
  <div class="actions"><%= f.submit "Pfollow" %></div>
<% end %>

Here’s the error from my log:

 Started POST "/prelationships" for 127.0.0.1 at 2011-11-20 23:31:57 +0100
 Processing by PrelationshipsController#create as HTML
 Parameters: {"utf8"=>"✓",       
 "authenticity_token"=>"NKqa1f0M2yPLQDHbRLnxl3SiwBeTus/1q1hpZjD7hgY=",      
 "prelationships"=>{"pfollowed_id"=>"5"}, "commit"=>"Pfollow"}
 Completed 500 Internal Server Error in 14ms

 NoMethodError (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.[]):
 app/controllers/prelationships_controller.rb:4:in `create'

 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-  
 3.0.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-   
 3.0.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb    
 (30.6ms)
 Rendered /Users/mmelone12/.rvm/gems/ruby-1.9.2-p290/gems/actionpack- 
 3.0.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within 
 rescues/layout (37.1ms)
  • 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-27T00:17:40+00:00Added an answer on May 27, 2026 at 12:17 am

    Yep. Like Swanand point out, you should initialize @project object inside :create action. Like @project = Project.find(params[:project_id]) if you do not do it with a before_filter.

    If you already instantiated @project before, see what happens when you manually try to retrieve @plant object in Rails console Plant.find(1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a test app where users can create ideas. I'm trying to
I'm working on an app where users can create an account and upload images,
I'm working on a Rails app that existing users can invite additional members to
I working on app what should get list of users from db and update
So, I'm working on an app where users can upload and manage photos with
I'm working on an app where users can share photos. The photos can optionally
So Im working on a rails app where users can comment on photos or
I'm working on an iPhone art app in which users can export their work
I'm working on a Rails 3 app that allows users to follow movies by
I'm new to rails and I'm working on a simple app where users create

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.