Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Active Record Associations in Rails Using Associations in Your App Rendering Collections

Employee > Salaries Modeling Question

Hello guys

Just a quick one.

How can i display a field below the worker to show employee paid salaries(list of all salaries paid to this employee) and then allow me to pay one single employee at a time?

<%= simple_form_for(@worker) do |f| %>
  <div class="form-inputs">
    <%= f.input :code %>
    <%= f.input :first_name %>
    <%= f.input :@worker.salaries[0] %>
    <%= f.input :nhif %>

Am trying to do this:

    <%= f.input :@worker(params[:id]).salaries %>


============
class Paycheck < ApplicationRecord
    belongs_to :worker
    belongs_to :salaries
end

class Salary < ApplicationRecord
  belongs_to :worker, optional: true
  has_many   :worker_salaries


class Worker < ApplicationRecord
    belongs_to :company, optional: true
    has_many :salaries, through: :worker_salaries
    has_many :worker_salaries
end

Help me model this effectively, if am getting it wrong.

MOD: I just added a bit of formatting to the code so people can read it more easily and be better able to help you! :-)

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

You'll need a model like this for a has_many through:; do you have one?

class WorkerSalary < ApplicationRecord
  belongs_to :worker
  belongs_to :salary
end

The Worker side is set up correctly, but I think you'll need to change Salary to:

class Salary < ApplicationRecord
  has_many :worker_salaries
  has_many :workers, through: :worker_salaries
end

Hello coach Mcavren

Thanks for chiming in. Yes the model part is solved. I have swithed back to using a one-many relationship (1 employee has many salaries). The struggle am facing is how to pull an employee togetherwith his salaries in the view and be able to pay him/her

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

I'm not an expert in simple_form, so you're on your own there, although you may find the associations and buttons sections of its documentation to be of interest.

Personally, I would probably just rely on a partial:

<%= render partial: "paychecks/_pay_button", collection: @worker.paychecks %>

Then, within views/paychecks/_pay_button.html.erb, you could render a link that routes to an action on the PaychecksController. The link could be styled to appear as a button using CSS. The controller action would need to find the Paycheck by its ID, and call a custom pay method on the Paycheck that marks it as paid in the database (and also does whatever other database operations are necessary). The Creating an Associated Record video should have most of the raw materials you need to make this work.