Created
February 2, 2017 12:07
-
-
Save niku/42edf138d299b7239d872705079b3dd2 to your computer and use it in GitHub Desktop.
ecto_sandbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User do | |
use Ecto.Schema | |
schema "users" do | |
field :name, :string | |
field :age, :integer, default: 0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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