Home Why is a join record being created for this model?
Reply: 0

Why is a join record being created for this model?

user3433 Published in August 15, 2018, 8:44 pm

This is a web app for scheduling our company's various site deployments among the developers and product owners on our Scrum team, when we launch updates or improvements. I have two models, User and Release. A Release is a single deployment event. Then I have a join table, called release_users. A user has and belongs to many releases. A release has many users, under the name product_owners, and belongs to one user, under the name developer. The Release table has a developer_id column which equals the User's ID, so it doesn't involve the ReleaseUser association at all (at least, that's what I want).

Here's the problem: when I create a new Release record (through the form), it automatically creates/saves a ReleaseUser record for the developer in addition to the product owners. When I look at @release.product_owners, I see the developer listed under there when it shouldn't be. It should only create ReleaseUser records for product owners. @release.developer works as expected, it selects the user from the User table based on developer_id and doesn't go through the ReleaseUser table.

So something is wrong in my model configurations I think? Rails is performing some behind-the-scenes magic that I can't pinpoint.



class Release < ActiveRecord::Base
  has_many :release_users, dependent: :destroy
  has_many :product_owners, :through => :release_users, :source => :user
  belongs_to :developer, :class_name => "User"


class User < ActiveRecord::Base
  has_many :release_users, dependent: :destroy
  has_many :releases, :through => :release_users


class ReleaseUser < ActiveRecord::Base
  belongs_to :release
  belongs_to :user


<div class="field">
  <%= f.label "Developer" %><br>
  <%= f.select :developer_id, options_for_select(User.select_options, selected: @release.selected_option), { include_blank: 'None' } %>

<div class="field">
  <%= f.label "Product Owners" %><br>
  <%= f.collection_check_boxes :product_owner_ids, User.all, :id, :full_name do |b| %>
    <label class="checkbox">
      <%= b.check_box %> <%= b.label %> <br/>
  <% end %>


# POST /releases
# POST /releases.json
def create
  @release = Release.new(release_params)

  # Release belongs to one developer at most
  unless release_params[:developer_id].empty?
    @developer = User.find(release_params[:developer_id])

    # User has many releases, add to array
    @developer.releases << @release

  respond_to do |format|
    if @release.save
      format.html { redirect_to releases_url, notice: 'Release was successfully created.' }
      format.json { render :show, status: :created, location: @release }
      format.html { render :new }
      format.json { render json: @release.errors, status: :unprocessable_entity }

(Rails 4)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300891 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO