Rails模型:保存子对象的数组

时间:2013-09-09 09:59:38

标签: ruby-on-rails ruby model rails-api

我是Ruby和Rails的新手。我正在尝试创建一个“团队”对象,该对象具有领导者ID和附加的用户数组。

问题 我无法将用户数组附加到团队对象 我无法定义leader对象,只存储其id

任何帮助非常感谢

My Rails Models:

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: [ ]
}

1 个答案:

答案 0 :(得分: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