Rails database table relational issue

9 views
Skip to first unread message

Mukto Mona

unread,
Jan 23, 2016, 7:39:55 AM1/23/16
to rubyonra...@googlegroups.com
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 %>

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 23, 2016, 8:28:41 AM1/23/16
to Ruby on Rails: Talk
On 23 January 2016 at 12:39, Mukto Mona <li...@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.

>
>
> 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])

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

Dave Aronson

unread,
Jan 23, 2016, 9:12:20 AM1/23/16
to rubyonrails-talk
On Sat, Jan 23, 2016 at 7:39 AM, Mukto Mona <li...@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.
Reply all
Reply to author
Forward
0 new messages