Monday 9 September 2013

Ruby Model: many sub objects as different alias?

Ruby Model: many sub objects as different alias?

I am new to ruby. I am try to create a "Team" object that has a leader (an
single user object). I would also like the object to have an array of
users (under the field users).
I am unsure how to define leader as an instance of user and users as an
array of users.
Any help greatly appreciated
My Ruby Model:
class Team
include Mongoid::Document
field :name, type: String
field :leader, type: Object
field :users, type: Array
has_one :user
has_many :users
end
controller action
# POST /teams # POST /teams.json
def create
new_params = params.permit( :name, :leader, :users)
@team = Team.new(new_params)
if @team.save
render json: @team, status: :created, location: @team
else
render json: @team.errors, status: :unprocessable_entity
end end
JSON Sent
{
leader: "522cf27114bc38307a000004"
name: "tester"
users: [522cf27114bc38307a000004, 522d966214bc38659300000d]
0: "522cf27114bc38307a000004"
1: "522d966214bc38659300000d"
}

No comments:

Post a Comment