0

Ruby と Rails は初めてです。リーダー ID とユーザーの配列が関連付けられた「チーム」オブジェクトを作成しようとしています。

問題 ユーザーの配列をチーム オブジェクトにアタッチできません リーダー オブジェクトを定義できず、その ID のみを保存します

どんな助けでも大歓迎です

私のRailsモデル:

class Team
      include Mongoid::Document
      include Mongoid::Timestamps::Created
      include Mongoid::Timestamps::Created

      field :name
      field :slug
      field :description

      field :leader
      field :users, type: Array

      field :holiday_days_per_year, type: Integer
      field :hours_per_day, type: Integer

      field :organisation_id, type: Integer

      embeds_many :users

      validates :name, :holiday_days_per_year, :presence => true
      validates :holiday_days_per_year, :hours_per_day, :numericality => true

      before_save :set_slug

      def set_slug
        self.slug = "#{name.parameterize}"
      end
    end

    class User
      include Mongoid::Document
      include Mongoid::Timestamps::Created
      include Mongoid::Timestamps::Created

      field :slug
      field :first_name
      field :last_name
      field :birth_date, type: Date

      field :job_title
      field :job_start_date, type: Date
      field :job_probation_ends, type: Date

      field :work_email
      field :work_address
      field :work_phone_number

      field :personal_email
      field :personal_address
      field :personal_phone_number

      field :organisation_id, type: Integer

      # emails should be unique
      validates_uniqueness_of :work_email, :personal_email

      validates :first_name, :last_name, :birth_date,
      :job_title, :job_start_date, :job_probation_ends,
      :work_address, :work_phone_number,
      :personal_address, :personal_phone_number,
      :presence => true

      # validates emails
      validates_format_of :work_email, :personal_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

      belongs_to :team, :inverse_of => :users

      before_save :set_slug

      def set_slug
        self.slug = "#{first_name.parameterize}-#{last_name.parameterize}"
      end
    end

コントローラーアクション

  # POST /teams
  # POST /teams.json
  def create
    new_params = params.permit(
        :name, :holiday_days_per_year, :hours_per_day, :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 送信済み

{
    holiday_days_per_year: 20
hours_per_day: 8
leader: "522cf27114bc38307a000004"
name: "Tester"
users: [
0: "522cf27114bc38307a000004"
1: "522d966214bc38659300000d"
2: "522dd21214bc38ac6b000011"
]
}

オブジェクトは作成されますが、ユーザーとリーダーは保存されず、オブジェクトは次のように返されます

{
_id: "522df8c714bc38ef3e000022",
created_at: "2013-09-09T16:35:19.405Z",
description: null,
holiday_days_per_year: 20,
hours_per_day: 8,
leader: "522d966214bc38659300000d",
name: "Tester",
organisation_id: null,
slug: "tester",
users: [ ]
}
4

1 に答える 1

0

モデルで、「as_json」メソッドを次のように再定義します

class Team < ActiveRecord::Base
 ..
 has_many :users

 def as_json(options)
   { :name=>self.name, :leader=>self.leader, :users=>self.users }
 end
end
于 2013-09-09T10:14:07.367 に答える