Simple_Form has_many through question

I am working on a project and I setup a has_many through relationship
but am having a hard time figuring out how to display and edit it on a
form.

What I have is a Training Table, an Organization Table, and a join table
called Attendees

In Attendees I have the training_id, organization_id, and attendee_count

Each training will have many Organization attendees

All I can seem to find in Simple Form is for selects with radio or
checkboxes, but I need to actually have an integer value which is the
attendee count per organization.

So the form should show All Organization names with the count from the
Attendee Table

Needless to say I am new to Rails and am struggling with the syntax.

John Sanderbeck

Attachments:
http://www.ruby-forum.com/attachment/11294/001_create_trainings.rb
http://www.ruby-forum.com/attachment/11295/002_create_organizations.rb
http://www.ruby-forum.com/attachment/11296/003_create_attendees.rb
http://www.ruby-forum.com/attachment/11297/attendee.rb
http://www.ruby-forum.com/attachment/11298/organization.rb
http://www.ruby-forum.com/attachment/11299/training.rb

I suggest that before continuing with your own project you work right
through a good tutorial such as railstutorial.org, which is free to
use online. That will show you the basics of rails, then you will be
able to answer basic questions like this yourself and will save
yourself a lot of time in the long run.

Colin

I have watched many tutorials through CodeSchool, Railscasts, and the
site you mentioned. None touch on exactly what I am trying to do.

I know it's something simple, I just can't find a reference to what I
want to do anywhere.

John

I have watched many tutorials through CodeSchool, Railscasts, and the
site you mentioned. None touch on exactly what I am trying to do.

I know it's something simple, I just can't find a reference to what I
want to do anywhere.

Firstly please quote the previous message when you reply, this is a
mailing list not a forum (though you may be accessing it via a forum
like interface) so if you do not quote the previous message we have to
look back through previous messages to get the context. Thanks.

I don't know about you but I find that watching tutorials is nowhere
near as good as working right through it oneself, doing the exercises,
finding out how to interpret errors when one makes copying errors and
so on, so I still suggest taking a few days out to work right through
railstutorial.org.

However, looking at your problem, I am not sure exactly what it is
that you cannot do. You said:

So the form should show All Organization names with the count from the
Attendee Table

What exactly is it that you do not know how to do? Is it accessing
the organisation names, determining the count of attendees, iterating
the names, displaying that information on the page or what? Also I
don't understand what simple form has got to do with just displaying
the data you describe. Forms are for inputting data, not just
displaying it.

Colin

Colin Law wrote in post #1182068:

Firstly please quote the previous message when you reply, this is a
mailing list not a forum (though you may be accessing it via a forum
like interface) so if you do not quote the previous message we have to
look back through previous messages to get the context. Thanks.

Sorry... I assumed that since the site was called ruby-forums that this
was a forum... :slight_smile:

I don't know about you but I find that watching tutorials is nowhere
near as good as working right through it oneself, doing the exercises,
finding out how to interpret errors when one makes copying errors and
so on, so I still suggest taking a few days out to work right through
railstutorial.org.

I will do that...

However, looking at your problem, I am not sure exactly what it is
that you cannot do. You said:

So the form should show All Organization names with the count from the
Attendee Table

What exactly is it that you do not know how to do? Is it accessing
the organisation names, determining the count of attendees, iterating
the names, displaying that information on the page or what? Also I
don't understand what simple form has got to do with just displaying
the data you describe. Forms are for inputting data, not just
displaying it.

I want to list every organization in a list (whether or not they have a
count) with a text entry box that is the attendance_count for that
organization. When I write I will just ignore the organizations that
have a count of zero. The attendance_count is in the Attendees table so
I can have a many_to_many relationship. I find all kinds of references
for radio buttons and check boxes with check_box_tag and
collection_check_box, but nothing for a number field. I assumed this was
a SImple Form syntax issue and not just a Ruby/Rails syntax issue.

As in

Training Name: [ ]
Training Date: [ ]

Attendance:

Colin Law wrote in post #1182068:

Firstly please quote the previous message when you reply, this is a
mailing list not a forum (though you may be accessing it via a forum
like interface) so if you do not quote the previous message we have to
look back through previous messages to get the context. Thanks.

Sorry... I assumed that since the site was called ruby-forums that this
was a forum... :slight_smile:

That is the site you are looking at but that is not the list, it is a
separate site that provides a forum like interface into the list. It
is entirely independent of the list. If you like a forum style
interface then by all means use that. Many much prefer the mailing
list format however.

I don't know about you but I find that watching tutorials is nowhere
near as good as working right through it oneself, doing the exercises,
finding out how to interpret errors when one makes copying errors and
so on, so I still suggest taking a few days out to work right through
railstutorial.org.

I will do that...

However, looking at your problem, I am not sure exactly what it is
that you cannot do. You said:

So the form should show All Organization names with the count from the
Attendee Table

What exactly is it that you do not know how to do? Is it accessing
the organisation names, determining the count of attendees, iterating
the names, displaying that information on the page or what? Also I
don't understand what simple form has got to do with just displaying
the data you describe. Forms are for inputting data, not just
displaying it.

I want to list every organization in a list (whether or not they have a
count) with a text entry box that is the attendance_count for that
organization. When I write I will just ignore the organizations that
have a count of zero. The attendance_count is in the Attendees table so
I can have a many_to_many relationship. I find all kinds of references
for radio buttons and check boxes with check_box_tag and
collection_check_box, but nothing for a number field. I assumed this was
a SImple Form syntax issue and not just a Ruby/Rails syntax issue.

As in

Training Name: [ ]
Training Date: [ ]

Attendance:
-------------------------
Organization #1: [ 0 ]
Organization #2: [ 20 ]
etc...

OK, I did not realise that you wanted to input a numeric value. The
fact that it is an integer rather than, for example, a text string, is
irrelevant to the form. You just need to use f.input. What you do
with that in the controller, of course, is up to you. You must
presumably validate the field accordingly.

I am not sure I would recommend that a beginner uses simple form. It
is a two edged sword. It does make some things very easy but because
you have not learned the basics of using a form whenever you need to
do something different you may not have the basic knowledge to handle
it.

Colin

Colin Law wrote in post #1182071:

OK, I did not realise that you wanted to input a numeric value. The
fact that it is an integer rather than, for example, a text string, is
irrelevant to the form. You just need to use f.input. What you do
with that in the controller, of course, is up to you. You must
presumably validate the field accordingly.

The issue is that I can't seem to get the association right. If I try
and access attendance_count I get an unknown method. I can access it
using Training.attendees.first.attendance_count, but cannot access it
using Training.organizations.first.attendance_count

I am not sure I would recommend that a beginner uses simple form. It
is a two edged sword. It does make some things very easy but because
you have not learned the basics of using a form whenever you need to
do something different you may not have the basic knowledge to handle
it.

I am not "that" much of a novice... I have setup quite a few forms that
work fine. It is this basically nested form that is difficult and most
of the tutorials don't cover it. I moved to simple_form hoping it would
be simpler. :smiley:

I even have a joined table for trainers that is a set of checkboxes and
that works fine. I just cannot for the life of me figure out how to do
the has_many through with a field in the through table.

I'll keep searching but I wanted to get it out on the list to see if
anyone had any suggestions.

John

attendance_count is not an attribute of organization, it is an
attribute of attendee so would need something like
Training.organizations.first.attendees.first.attendance_count
An organization has many attendees so for each organization there are
many values of attendance_count. Which makes me realise that the
table example you posted does not make sense, as each organization
must have multiple rows. If that is not the case then I do not
understand your associations.

Perhaps you had better tell us exactly what associations you have
setup, in terms of has_many, belongs_to has_many_many through etc.
Tell us what you have declared for each model.

Colin

Colin Law wrote in post #1182080:

Perhaps you had better tell us exactly what associations you have
setup, in terms of has_many, belongs_to has_many_many through etc.
Tell us what you have declared for each model.

The associations are in the file attachments at the head of this post.

I have

  1. a training that has_many attendees and has_many organizations
through attendees.
  2. a organization that has many attendees and has_many trainings
through attendees
  3. a join table of attendees with a training_id, organization_id, and
attendance_count

  Looking though my code just now I noticed that I had an ID on the
attendees join which I shouldn't have and did not have a unique index. I
just fixed that however I don't believe that has anything to do with my
problem.

  I guess my issue is that I don't know the proper way to display the
form to allow adding attendance to the training by displaying all the
organizations with a text box beside the name to add the count for each
organization.

John

With those relationships an organization does not have an
attendance_count, it has_many attendees and each attendee contains an
attendance_count. An organization has associated with it many counts,
not one. So how can you have a box to enter an attendance_count for
an organization?

Colin

Colin Law wrote in post #1182080:

attendance_count is not an attribute of organization, it is an
attribute of attendee so would need something like
Training.organizations.first.attendees.first.attendance_count
An organization has many attendees so for each organization there are
many values of attendance_count. Which makes me realise that the
table example you posted does not make sense, as each organization
must have multiple rows. If that is not the case then I do not
understand your associations.

I think I have an idea on how I need to do this...

If I add a scope to Organization that is like the following, would this
work?

scope :attendance, lambda {|trainingid|
joins(:attendees).where("training_id = ?", trainingid).attendance_count

Would this make sense?

or would this be better in a method like

def self.attendance(trainingid)
  joins(:attendees).where("training_id = ?",trainingid).attendance_count
end

I just tried it both ways though and I get an undefined method
attendance for the scope and an unknown method joins for the method so
maybe my syntax is incorrect...

John

It is very unusual to need joins. Assuming you are trying to get the
attendance count for a particular organization at a particular
training then you want something like
self.attendees.where(training_id: @training.id).first.attendance_count
or the equivalent in a scope.

You need the .first as the result of where will be (effectively) an
array even if there is only one. Don't forget to allow for the
situation where there are no matching attendees (if that can happen)
in which case the array will be empty so the call to first will return
nil and the attempt to reference attendance count will fail. One way
to get over that is to use 'try'.

Colin