Skip to content

Instantly share code, notes, and snippets.

@niku
Created February 2, 2017 12:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niku/42edf138d299b7239d872705079b3dd2 to your computer and use it in GitHub Desktop.
Save niku/42edf138d299b7239d872705079b3dd2 to your computer and use it in GitHub Desktop.
ecto_sandbox
defmodule EctoSandbox.Mixfile do
use Mix.Project
def project do
[app: :ecto_sandbox,
version: "0.1.0",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
elixirc_paths: ["."],
deps: deps()]
end
def application do
[extra_applications: [:logger]]
end
defp deps do
[{:ecto, "~> 2.1"}]
end
end
defmodule User do
use Ecto.Schema
schema "users" do
field :name, :string
field :age, :integer, default: 0
end
end
defmodule Weather do
use Ecto.Schema
schema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0 # 降水量
end
def changeset(weather, params \\ %{}) do
weather
|> Ecto.Changeset.change(params)
|> Ecto.Changeset.validate_required([:city, :temp_lo, :temp_hi])
|> Ecto.Changeset.validate_number(:prcp, greater_than_or_equal_to: 0.0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment