0

I have the following models:

class Package < ActiveRecord::Base
  # relationships
  belongs_to :company

  has_many :activitie_package

  accepts_nested_attributes_for :activity_package

  # validations
  validates_presence_of :company
end



class ActivityPackage < ActiveRecord::Base
  belongs_to :activity
  belongs_to :package
  belongs_to :company
end



class Activity < ActiveRecord::Base
    # relationships
    belongs_to :company
end

I have a table between the has_many relationship because the Activity.price can change the value in a diff pack.

My need is to list all Activity as a checkbox in a table, like this:

table {
  width: 100%;
}
<table>
  <tr>
     <td></td>
     <td>Activity</td>
     <td>Price</td>
  </tr>
  <tr>
     <td><input type="checkbox" name="package[activity_package_attributes][{{$index}}][activity_id]" /></td>
     <td>Swimming</td>
     <td>$50</td>
  </tr>
  <tr>
     <td><input type="checkbox" name="package[activity_package_attributes][{{$index}}][activity_id]" /></td>
     <td>Water Aerobics</td>
     <td>$90</td>
  </tr>
</table>

First: How I'll handle the update and delete operation? Should I delete all relationship and insert all the selected activities again?

Second: Can I add a alias to my has_many :activity_package? eg.: package.activies, but the relationship being the activity_package model.

I'll really appreciate some opinion to this with a different way :)

Update Searching, I found this answer. Isn't there a better solution to that?

Community
  • 1
  • 1
  • 1
    Check out this question I ended up answering for myself. It has a bunch of AJAX going on, but at the core it's a collection of checkboxes. http://stackoverflow.com/questions/21896632/submit-ul-list-as-parameter-array-in-rails-4-form-adding-params-values-to-par – Beartech Apr 07 '15 at 03:16
  • 1
    You can use the has_many_through relation to create the checkboxes and then you can later update them using standard Rails actions. Rails can handle it with a couple caveats. watch this: http://railscasts.com/episodes/17-habtm-checkboxes – Beartech Apr 07 '15 at 03:20
  • 1
    And finally check out this answer http://stackoverflow.com/a/21688201/2727267 It has info on why you need a hidden field to get updates to work. – Beartech Apr 07 '15 at 03:21
  • Thank you Beartech. I really need to apologize my delay to answer. I'll read those links and feedback if I got! Just one thing I forgot to say, is that i'm using angularjs to build the form. – heber gentilin Apr 10 '15 at 00:46
  • I need something like [this](https://railsforum.com/topic/332-how-to-add-additional-attributes-to-the-join-table-in-habtm-and-fetch-that-when-getting-association-values/?p=1003), and the ```price``` inside the join table - see the table in the question. – heber gentilin Apr 10 '15 at 01:47

0 Answers0