Forum: Ruby on Rails Rails database table relational issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Afaea01ef63a5e5d201b6799a9d5fab2?d=identicon&s=25 Mukto Mona (mukto_mona)
on 2016-01-23 13:39
I am a newbie in ruby on rails it's also my first ruby application. The
problem is database relation I am trying to make relation within two
tables and the same procedure are working properly in another relation
but here it's showing an error.

The error is:

    'undefined method `create_applied_jobs' for nil:NilClass'


Here are my codes:

applied_job.rb

   class AppliedJob < ActiveRecord::Base
     belongs_to :jobseekers
   end

jobseeker.rb

   class Jobseeker < ActiveRecord::Base
      has_one :applied_job
   end

view_details_controller.rb

   def create
      params.permit!
      if @applied_job =
      @current_user.create_applied_jobs(params[:applied_job])
        flash[:notice] = "You have applied successfully"
        render "viewDetails"
     else
        render "viewDetails"
        flash[:warning] = "Please try agian"
    end
 end

sessions_helper.rb

    def current_user
       @current_user ||= Jobseeker.find_by(jobseeker_id:
session[:user_id])
    end

viewDetails.html.erb

    <%= form_for :applied_job, url: viewDetails_path(@applied_job),
action: :create, method: :post  do |f| %>
      <ul class="form-style-1">
        <li><label>Preferred Joining Date<span
class="required">*</span></label>
            <%= f.text_field :preffered_joining_date, class:
'field-long', id: 'datepicker', placeholder: 'Preferred Joining Date'%>
        </li>
        <li>
            <label>Expected Salary <span
class="required">*</span></label>
             <%= f.text_field :expected_salary, class: 'form-control',
placeholder: 'Expected Salary'%>
        </li>
        <li>
             <input type="submit" value="Submit" />
        </li>
    </ul>
 <% end %>
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-01-23 14:29
(Received via mailing list)
On 23 January 2016 at 12:39, Mukto Mona <lists@ruby-forum.com> wrote:
> I am a newbie in ruby on rails it's also my first ruby application. The
> problem is database relation I am trying to make relation within two
> tables and the same procedure are working properly in another relation
> but here it's showing an error.
>
> The error is:
>
>     'undefined method `create_applied_jobs' for nil:NilClass'

If you look more carefully at the stack trace (in the server window or
in log/development.log) you should be able to see which line of your
code caused the problem.

> jobseeker.rb
>       @current_user.create_applied_jobs(params[:applied_job])
I am assuming that it this line that has generated the error.  The
error means that you have called create_applied_jobs on a variable
that is nil, so @current_user is nil.  Perhaps you have not called
current_user, I don't see where you are calling current_user to set it
up.  In fact I am not sure why you don't just have
current_user.create_applied_jobs(params[:applied_job])
though I have not looked in great detail at your code so there may be a
reason.

Colin
9dbb61df49ea99c18e8460b8b2e8c1b9?d=identicon&s=25 Dave Aronson (Guest)
on 2016-01-23 15:13
(Received via mailing list)
On Sat, Jan 23, 2016 at 7:39 AM, Mukto Mona <lists@ruby-forum.com>
wrote:

>     'undefined method `create_applied_jobs' for nil:NilClass'
...
>    class Jobseeker < ActiveRecord::Base
>       has_one :applied_job
>    end

In addition to what Colin has said about setting up @current_user, you
may need to change the method call.

Since this is a has_one relationship rather than has_many, it might
need "create_applied_job" (i.e., singular) instead.  (Though it seems
to me you probably want to change the has_one to has_many instead, to
allow seekers to apply to multiple jobs.  Possibly even :through some
kind of JobApplication model, where you can store the date, status,
etc.)

-Dave

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
This topic is locked and can not be replied to.