Skip to content

Instantly share code, notes, and snippets.

@joshchernoff joshchernoff/console
Created Mar 12, 2017

Embed
What would you like to do?
1) test it inserts with a valid changeset (Pricer.Product.PastPriceRecordTest)
test/pricer/product/past_price_record_test.exs:11
** (ArgumentError) unknown field `product_id`. Only fields, embeds and associations (except :through ones) are supported in changesets
stacktrace:
(ecto) lib/ecto/changeset.ex:503: Ecto.Changeset.type!/2
(ecto) lib/ecto/changeset.ex:474: Ecto.Changeset.process_param/8
(elixir) lib/enum.ex:1325: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:450: Ecto.Changeset.do_cast/7
(pricer) lib/pricer/product/past_price_record.ex:15: Pricer.Product.PastPriceRecord.changeset/2
test/pricer/product/past_price_record_test.exs:13: (test)
defmodule Pricer.Repo.Migrations.CreatePastPriceRecords do
use Ecto.Migration
def change do
create table(:past_price_records) do
add :product_id, references(:products), null: false
add :price, :integer
add :percentage_change, :float
timestamps
end
create index(:past_price_records, [:product_id])
end
end
defmodule Pricer.Product.PastPriceRecord do
use Ecto.Schema
import Ecto.Changeset
schema "past_price_records" do
field :price, :integer
field :percentage_change, :float
belongs_to :product, Pricer.Product, foreign_key: :product_id
timestamps
end
@fields ~w(product_id price percentage_change)a
def changeset(data, params \\ %{}) do
data
|> cast(params, @fields)
|> validate_required(@fields)
|> validate_number(:price, greater_than_or_equal_to: 0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.