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


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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/514907ea2ddbb268460235122fcae9b8%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Colin Law at Mar 9, 2016 at 2:49 pm

    On 9 March 2016 at 14:43, John Sanderbeck wrote:
    ...
    Needless to say I am new to Rails and am struggling with the syntax.
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt6o8Y0cVX49RV3o10-J_ZftFoJfA_YNEbhUWhafLg7Mw%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Sanderbeck at Mar 9, 2016 at 4:43 pm
    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/744b442016cdaef12d78a13067ed2631%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 9, 2016 at 8:35 pm

    On 9 March 2016 at 16:43, John Sanderbeck wrote:
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtrZMMuHOOFXHA04GQ%2B6T-kxrnOvYzOk6LCQow0XcvXog%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Sanderbeck at Mar 9, 2016 at 9:06 pm

    Colin Law wrote in post #1182068:
    On 9 March 2016 at 16:43, John Sanderbeck wrote:
    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... :-)
    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...

    John

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/536ff9e515665563f57f7c13ad0c0ca8%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 9, 2016 at 9:24 pm

    On 9 March 2016 at 21:06, John Sanderbeck wrote:
    Colin Law wrote in post #1182068:
    On 9 March 2016 at 16:43, John Sanderbeck wrote:
    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... :-)
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsr9UwObNqx5VYE-TWa2SLqqniOVmtZOCd_QoxsqDxkhA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Sanderbeck at Mar 9, 2016 at 11:19 pm

    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. :-D

    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/35ab233991302faecd8983abfe1ef665%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 10, 2016 at 8:18 am

    On 9 March 2016 at 23:19, John Sanderbeck wrote:
    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
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuRmov_ADS_RNgpG0HN3mqSYFR1u%2B7d6-TYJD8JDOSugw%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Sanderbeck at Mar 10, 2016 at 11:26 am

    Colin Law wrote in post #1182080:
    On 9 March 2016 at 23:19, John Sanderbeck wrote:
    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/daf966b174431a5ab8798e27e62f060e%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 10, 2016 at 2:52 pm

    On 10 March 2016 at 11:26, John Sanderbeck wrote:
    Colin Law wrote in post #1182080:
    On 9 March 2016 at 23:19, John Sanderbeck wrote:
    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.
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLskgRWsQTN_nE_WCTySMTH%3DBwmkrb9%3DiSawCZDKBgsgfA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Sanderbeck at Mar 10, 2016 at 3:06 pm

    Colin Law wrote in post #1182080:
    On 9 March 2016 at 23:19, John Sanderbeck wrote:
    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c948eb5c20201f7b3ce472b9eed876b2%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 10, 2016 at 4:42 pm

    On 10 March 2016 at 15:05, John Sanderbeck wrote:
    Colin Law wrote in post #1182080:
    On 9 March 2016 at 23:19, John Sanderbeck wrote:
    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...
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvRCg%2B_ZKBL91d3SrWu3yhM1SEeV-uoVthtm-_hUXMWZA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 9, '16 at 2:43p
activeMar 10, '16 at 4:42p
posts12
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

John Sanderbeck: 6 posts Colin Law: 6 posts

People

Translate

site design / logo © 2021 Grokbase