Asherlc
Asherlc

Reputation: 1161

Controller not creating with association

I've got two models, Users and Organizations, which have a has_many relationship using an assignments table. I have a nested resource form when the user is created, which creates an associated organization just fine. However, when creating an organization, it doesn't associate it with the user.

Here's my relevant Organizations controller code:

  def new
    @organization = current_user.organizations.build
  end

  def create
    @organization = current_user.organizations.build(params[:organization])
    @organization.save
  end

And my models:

Organizations Assignments

class OrganizationAssignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :organization

  attr_accessible :user_id, :organization_id
end

Organizations:

class Organization < ActiveRecord::Base
  validates :subdomain, :presence => true, :uniqueness => true

  has_many :organization_assignments
  has_many :people
  has_many :users, :through => :organization_assignments

  attr_accessible :name, :subdomain
end

Users:

class User < ActiveRecord::Base

  has_many :organization_assignments
  has_many :organizations, :through => :organization_assignments

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  accepts_nested_attributes_for :organizations

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :organizations_attributes
  # attr_accessible :title, :body

end

form view:

= form_for @organization, :html => { :class => 'form-horizontal' } do |f|
  - @organization.errors.full_messages.each do |msg|
    .alert.alert-error
      %h3
        = pluralize(@organization.errors.count, 'error')
        prohibited this user from being saved:
      %ul
        %li
          = msg

  = f.label :name
  = f.text_field :name

  = f.label :subdomain
  = f.text_field :subdomain

  .form-actions
    = f.submit nil, :class => 'btn btn-primary'
    = link_to t('.cancel', :default => t("helpers.links.cancel")), organizations_path, :class => 'btn'

I'm able to associate the organizations fine after the fact in the console, so I'm pretty sure the relationships are set up correctly in the model. Is there anything else I'm missing?

Upvotes: 0

Views: 82

Answers (1)

deefour
deefour

Reputation: 35360

From my experience with Rails, you can't expect the relation to be made that way. Try something like this.

def create
  @organization = Organization.build(params[:organization])
  @organization.save

  current_user.organizations << @organization
end

You might alternatively keep your code as-is, but save current_user instead of @organization.

def create
  @organization = current_user.organizations.build(params[:organization])
  current_user.save
end

Upvotes: 1

Related Questions