Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Belongs to association

Michael edited this page · 7 revisions

Synopsis: Rails 3

class Player < ActiveRecord::Base
  belongs_to :team, :inverse_of => :players

  # if you want a dropdown select: (natural choice for a belongs_to association)

    attr_accessible :team_id

  # or for nested fields: 

    attr_accessible :team_attributes
    accepts_nested_attributes_for :team, :allow_destroy => true

  rails_admin do
    configure :team do
      # configuration here
    end
  end
end

# for info
class Team < ActiveRecord::Base
  has_many :players, :inverse_of => :team
end

Rails 4 has no attr_accessible

class Player < ActiveRecord::Base
  belongs_to :team, :inverse_of => :players   # dropdown select: belongs_to association
  # or for nested fields: 
  accepts_nested_attributes_for :team, :allow_destroy => true
end

# for info
class Team < ActiveRecord::Base
  has_many :players, :inverse_of => :team
end

More here

Something went wrong with that request. Please try again.