Hello, I am new to Rails and I'm building an API.
I have 2 objects:
Team: name, leader (user object, person who manages the team), users (array of user objects for people in the team (not including the leader))
User: id, name, email, typical fields
Currently my front end sends back a team object as name, users (array of user ids), leader (user id).
My Create and Update look like this for team:
@team = Team.find(params[:id])
render json: @team
render json: @team.errors, status: :unprocessable_entity
My Team model looks like this:
has_one :leader, class_name: 'User'
has_many :users, inverse_of: :team
# define the users based on an array of people with the team id
user_ids.each do |id|
User.find(id).update_attribute :team_id, self.id
My User model looks like this:
## Database authenticatable
field :email, :type => String, :default => ""
# users belong to a team
belongs_to :team, inverse_of: :user
I have a team serialiser like this:
class TeamSerializer < ActiveModel::Serializer
attributes :_id, :name
The problem I have is that when I save the team (or create it), the leader object never gets saved, it seems to default leader to the first item in the users array.
Can anyone help me get the relationships right? Many thanks for any assistance.