has_many :through

35 views
Skip to first unread message

Bruno Walraven

unread,
Apr 7, 2015, 5:45:28 AM4/7/15
to rubyonra...@googlegroups.com
Hi all,

I've been stuck trying to get a has_many :through relationship working on my Rails application.

I have a Employee model, a Timesheet model and a Payment model.
The Employee has many Timesheets and a Timesheet has many Employees. 
The Timesheet table will contain its ID and a Date. the Payment model is the join table, it belongs to a Employee and a Timesheet, it also has an extra field called hours_worked.

See below models

class Timesheet < ActiveRecord::Base
    belongs_to :user
    has_many :payments
    has_many :employees, :through => :payments
end

class Employee < ActiveRecord::Base
    belongs_to :user
    has_many :payments
    has_many :timesheets, :through => :payments   
end

class Payment < ActiveRecord::Base   
    belongs_to :employee
    belongs_to :timesheet
end

I want to create a form where I can create a new Timesheet by chosing a date, listing all Employees and allowing me to enter the hours_worked for each employee.
Once I submit the form, it should create a new Timesheet, and create a Payment for each employee with each employee's hours_worked for that specific Timesheet.
I hope this makes sense and I can get your help!! 
Thanks

Colin Law

unread,
Apr 7, 2015, 6:01:08 AM4/7/15
to rubyonra...@googlegroups.com
So what is not working, or which bit don't you know how to do?

Have you worked right through a good tutorial such as railstutorial.org?

Colin

Bruno Walraven

unread,
Apr 7, 2015, 6:23:11 AM4/7/15
to rubyonra...@googlegroups.com
Hi Colin,

What I don't know how to do is the nested form, and the new and created methods in the controller for this specific association.
I have tried the relationship on the rails console and it works fine.

I actually have done the rails tutorial and had a good look through stackoverflow, but i still can't get my head around this one.

Appreciate your help!
Message has been deleted

Bruno Walraven

unread,
Apr 7, 2015, 7:31:32 AM4/7/15
to rubyonra...@googlegroups.com
My intention is to create a form along those lines:
<%= form_for(@timesheet, :html => {:multipart => true}) do |f| %>
   
   
<!--creates a new timesheet -->
   
<%= f.label "Enter date for timesheet" %>
   
<%= f.text_field :date %>
     
   
<%= fields_for :payment do |p| %>
   
<!-- Add worked hours for each employee -> @employee = Employee.all on controller (new method)-->
   
<% @employee.each do |t| %>
         
<%= t.fname  %>
         
<br />
         
<%= f.label "Basic Hours" %>
         
<%= p.text_field :hours  %>
         
<br />
   
<% end %>

   
<%= f.submit 'Submit', :class => 'btn btn-primary' %>

<% end %>

My issue is I can't find a way to get this form to create the timesheet and the payment for each employee.

Hopes this makes it a bit easier to understand.

Thanks



On Tuesday, April 7, 2015 at 11:01:08 AM UTC+1, Colin Law wrote:

Dave Aronson

unread,
Apr 7, 2015, 8:36:55 AM4/7/15
to rubyonrails-talk
On Tue, Apr 7, 2015 at 5:41 AM, Bruno Walraven <brunow...@gmail.com> wrote:

> I have a Employee model, a Timesheet model and a Payment model.
> The Employee has many Timesheets and a Timesheet has many Employees.

Are you sure you don't mean a Timesheet has *one* Employee?

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

Bruno Walraven

unread,
Apr 7, 2015, 8:41:07 AM4/7/15
to rubyonra...@googlegroups.com
Do you mean by having a Timesheet that has a employee id, the date and the hours worked?

Dave Aronson

unread,
Apr 7, 2015, 8:45:16 AM4/7/15
to rubyonrails-talk
On Tue, Apr 7, 2015 at 8:41 AM, Bruno Walraven
<brunow...@gmail.com> wrote (rearranged to proper order):

> On Tuesday, April 7, 2015 at 1:36:55 PM UTC+1, Dave Aronson wrote:
>>
>> On Tue, Apr 7, 2015 at 5:41 AM, Bruno Walraven <brunow...@gmail.com>
>> wrote:
>>
>> > I have a Employee model, a Timesheet model and a Payment model.
>> > The Employee has many Timesheets and a Timesheet has many Employees.
>>
>> Are you sure you don't mean a Timesheet has *one* Employee?

> Do you mean by having a Timesheet that has a employee id, the date and the
> hours worked?

Yes, that's how timesheets usually work in the real world. You may be
trying to do something different in your app, if you think it will
make the program better. I'm just trying to clarify....

Bruno Walraven

unread,
Apr 7, 2015, 8:53:00 AM4/7/15
to rubyonra...@googlegroups.com
Thanks for that Dave! Sometimes we over complicate stuff when learning :)

I just need to look into creating a form that will allow me to select a date for the timesheet, and then loop through all employees on the database to give me a option to create a timesheet for each employee using that specific date, all in one form.
The form will also allow me to enter the hours worked for each employee. When I submit the form, it should create a timesheet for each employee with their worked hours for that specific date.

a little help with the form and new/create methods would be a great help!

Thanks again...
Reply all
Reply to author
Forward
0 new messages