在嵌套资源中传递来自祖父母的值

时间:2018-01-06 14:56:02

标签: elixir phoenix-framework

我有三个模特 - 活动,注册和参与者。活动包含费用信息,将根据参与者的年龄进行申请。我试图保存注册和参与者的单一呼叫 - 嵌套资源。在保存表单时,应根据参与者的年龄选择费用信息并保存在参与者记录中。费用信息需要来自registration.event。我怎样才能做到这一点?

相关代码:

event.ex:

schema "events" do
    field :additional_information_url, :string
    field :description, :string
    field :email, :string
    field :fees_first, :decimal
    field :fees_second, :decimal
    field :fees_third, :decimal
    field :invitation_url, :string
    field :start_date, :date
    field :end_date, :date
    field :permalink, :string
    field :registrations_enabled, :boolean, default: true
    field :title, :string
    field :transport_url, :string
    has_many :registrations, Eventyr.Registrations.Registration
    timestamps()
end

registration.ex:

schema "registrations" do
    field :conf_number, :string
    belongs_to :event, Eventyr.Events.Event
    has_many :participants, Participant
    timestamps()
end

def changeset(%Registration{} = registration, attrs) do
    registration
    |> cast(attrs, [:conf_number, :event_id])
    |> validate_required([:conf_number, :event_id])
    |> cast_assoc(:participants, required: true)
    |> unique_constraint(:conf_number)
end

participant.ex:

schema "participants" do
    field :age, :integer
    field :amount_paid, :decimal, default: 0
    field :conf_number, :integer
    field :email, :string
    field :email_confirmation, :string, virtual: true
    field :fees, :decimal
    field :first_name, :string
    field :gender, :integer, default: 1
    field :id_number, :string
    field :last_name, :string
    field :phone_number, :string
    belongs_to :registration, Registration
    belongs_to :country, Eventyr.Regions.Country
    timestamps()
end

@doc false
def changeset(%Participant{} = participant, attrs) do
    participant
    |> cast(attrs, [:conf_number, :primary, :first_name, :last_name,
                :id_number, :arrival_date, :departure_date, :gender,
                :age, :is_preceptor, :city, :prefect_name, :status,
                :email, :email_confirmation, :amount_paid, :first_visit,
                :phone_number, :emergency_contact_phone,
                :arrival_time, :departure_time, :accommodation_choice,
                :transport_required, :country_id
                :comment, :admin_comment])
    |> populate_fees
    |> validate_required([:conf_number, :primary, :first_name, :last_name,
                      :arrival_date, :departure_date, :gender, :age,
                      :is_preceptor, :city, :status, :email, :amount_paid,
                      :first_visit, :phone_number, :emergency_contact_phone,
                      :arrival_time, :departure_time, :accommodation_choice,
                      :transport_required, :country_id])
    |> validate_inclusion(:age, 0..100)
    |> validate_length(:email, min: 6, max: 150)
    |> validate_format(:email, ~r/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
    |> validate_confirmation(:email)
    |> unique_constraint(:conf_number,
                     name: :participants_conf_number_registration_id_index,
                     message: "Conf # and Participant ID should be unique")
end

defp populate_fees(%Ecto.Changeset{data: %{id: id}} = changeset)
                                  when is_integer(id) do
  changeset
end

defp populate_fees(%{params: %{"event" => event}} = changeset) do
  changeset
  |> put_change(:fees,  case get_change(changeset, :age) do
          age when age in 0..5  -> event.fees_first
          age when age in 6..16 -> event.fees_second
          age when age > 16     -> event.fees_third
        end)
end

defp populate_fees(changeset) do
    changeset
end

如果这是正确的方法,我如何在参与者中获取事件上下文或者推荐的方式是什么?

0 个答案:

没有答案