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?