Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The has_and_belongs_to_many method of joining models together is deprecated in favor of the new has_many ... :through approach. It is very difficult to manage the data stored in a has_and_belongs_to_many relationship, as there are no default methods provided by Rails, but the :through method is a first-class model and can be manipulated as such.</p> <p>As it relates to your problem, you may want to solve it like this:</p> <pre><code>class Competition &lt; ActiveRecord::Base has_many :participating_teams has_many :teams, :through =&gt; :participating_teams, :source =&gt; :team end class Team &lt; ActiveRecord::Base has_many :participating_teams has_many :competitions, :through =&gt; :participating_teams, :source =&gt; :competition end class ParticipatingTeam &lt; ActiveRecord::Base belongs_to :competition belongs_to :team end </code></pre> <p>When it comes to creating the teams themselves, you should structure your form so that one of the parameters you receive is sent as an array. Typically this is done by specifying all the check-box fields to be the same name, such as 'competitions[]' and then set the value for each check-box to be the ID of the competition. Then the controller would look something like this:</p> <pre><code>class TeamsController &lt; ApplicationController before_filter :build_team, :only =&gt; [ :new, :create ] def new @competitions = Competitions.all end def create @team.save! # .. usual if logic on save rescue ActiveRecord::RecordInvalid new render(:action =&gt; 'new') end protected def build_team # Set default empty hash if this is a new call, or a create call # with missing params. params[:team] ||= { } # NOTE: HashWithIndifferentAccess requires keys to be deleted by String # name not Symbol. competition_ids = params[:team].delete('competitions') @team = Team.new(params[:team]) @team.competitions = Competition.find_all_by_id(competition_ids) end end </code></pre> <p>Setting the status of checked or unchecked for each element in your check-box listing is done by something like:</p> <pre><code>checked = @team.competitions.include?(competition) </code></pre> <p>Where 'competition' is the one being iterated over.</p> <p>You can easily add and remove items from your competitions listing, or simply re-assign the whole list and Rails will figure out the new relationships based on it. Your update method would not look that different from the new method, except that you'd be using update_attributes instead of new.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload