I am trying to create a resume builder. Inside the resume, I would like to display the first corresponding Job
title to a specific User
in the show.html.erb
.
I first made sure that Jobs has the user_id foreign key..
create_table "jobs", force: true do |t|
t.string "title"
t.string "company"
t.date "date_start"
t.date "date_end"
t.boolean "current"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "career_id"
end
..and the relationships are like so:
class User < ActiveRecord::Base
has_one :career
has_many :jobs, through: :career
end
class Job < ActiveRecord::Base
belongs_to :user
has_many :descriptions
end
def show
@user = User.find(1)
end
What is the best way to go about this? I was able to display other elements such as Name and Contacts on the same Show View page. I have tried a ton of different lines but currently have this...
<%= @user.jobs.first.title %>
Your Job model has belongs_to :user, which would mean your jobs table needs a user_id attribute.
I don't know how your Career model looks like, but it seems you don't need
has_many :jobs, through: :career
if you're tying the job directly to the user via user_id (which should be added to your jobs table). In other words,
has_many :jobs
might just work. However, if you need to stick to Career, then make sure that
class Career < ActiveRecord::Base
belongs_to :user
has_many :jobs
end
And then, from your view do:
<%= @user.jobs.first.title %>