Code Monkey home page Code Monkey logo

ecto_resource's People

Contributors

daytonn avatar daytonnzr avatar dependabot[bot] avatar joseph-lozano avatar rosston avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ecto_resource's Issues

Multiple resources (schemas) per context

Hello,

First of all, you did a great job on this library. Thanks so much for sharing it.

I have a question related to using multiple schemas in a single context. Is it possible? The documentation states that it's doable, but there isn't any example on how to accomplish this.

To give an example of what I'm trying to accomplish:

Let's say we have an Account context backed by two schemas, User and Profile.

Can both be exposed as resources using this library, on the Account context?

So we'd get:

Account.all_users
Account.create_user
#...
Account.all_profiles
Account.create_profile
#...

If this isn't doable or out of the scope of this library, it's totally ok. I'm just starting to get my head around Elixir and it's conventions, and maybe I'm thinking about this problem in the wrong way.

Thanks again for the great work! This lib is really usefull to reduce boilerplate.

Empty Changeset function throws error

I have a Books Context with the Book resource. On the BookController I have this code.

def new(conn, _params) do
  changeset = Books.change_book(%{})
  render(conn, "new.html", changeset: changeset)
end

But calling this throws

iex(7)> IndiePaper.Books.change_book(%{})
** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast/4

    The following arguments were given to Ecto.Changeset.cast/4:

        # 1
        %{}

        # 2
        %{}

        # 3
        [:title, :description]

        # 4
        []

    Attempted function clauses (showing 5 out of 5):

        def cast(_data, %{__struct__: _} = params, _permitted, _opts)
        def cast({data, types}, params, permitted, opts) when is_map(data)
        def cast(%Ecto.Changeset{types: nil}, _params, _permitted, _opts)
        def cast(%Ecto.Changeset{changes: changes, data: data, types: types, empty_values: empty_values} = changeset, params, permitted, opts)
        def cast(%{__struct__: module} = data, params, permitted, opts)

    (ecto 3.5.6) lib/ecto/changeset.ex:466: Ecto.Changeset.cast/4
    (indie_paper 0.1.0) lib/indie_paper/books/book.ex:17: IndiePaper.Books.Book.changeset/2

Any idea on how to fix this ?

Here is my Books Context for reference

defmodule IndiePaper.Books do
  alias IndiePaper.{Repo}

  use EctoResource

  using_repo(Repo) do
    resource(IndiePaper.Books.Book)
  end
end

Here is the Book Schema

defmodule IndiePaper.Books.Book do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "books" do
    field :title, :string
    field :description, :string

    timestamps()
  end

  @doc false
  def changeset(book, attrs \\ %{}) do
    book
    |> cast(attrs, [:title, :description])
    |> validate_required([:title, :description])
    |> unique_constraint(:title)
  end
end

Vote of Thanks ๐Ÿš€

Thanks for making such an awesome library. I had always been put off by the amount of boilerplate that I had to work into Phoenix contexts for all the simple crud operations. This library cleaned up my entire context in a jiffy. Thanks so much for making such a nice and awesome library. It's not every day you find something that does what you want to do the way it's supposed to be done and nothing more.

Thanks so much for making this and more for making it available to the world. Have a great day (every day) and a great life. Cheers :D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.