wizardsnero.blogg.se

Elixir ecto table names plural or singular
Elixir ecto table names plural or singular











elixir ecto table names plural or singular
  1. #Elixir ecto table names plural or singular how to#
  2. #Elixir ecto table names plural or singular update#

There's a similar thing going on with users - while there's no logins, I wanted to both be able to identify players throughout a session and let them set a nice, human-friendly nickname.

elixir ecto table names plural or singular

All games are identified by an ID, which is a random string.

#Elixir ecto table names plural or singular update#

The `GameStore` has functions to retrieve and update existing game states as well as a convenience `get_or_create` function, which returns a game if it already exists or creates a new one if it doesn't. The way it all works is that I pass `GameState` structs around, each one representing a single game. when I deploy) is definitely not ideal - but I never got to solving it. Losing the state of all games on restart (e.g. I thought trying to preserve some state across restarts might be an interesting experiment. One cool thing about ETS, though, is that it should be pretty easy to swap with DETS, which is disk-based. I might experiment with doing that later. It would've been better, probably, to isolate them so each game would have its own GenServer. It works, but it's not isolated - if anything goes wrong with a game it will affect all the others too. What I ended up with is not ideal - I have a single GenServer, called `GameStore`, responsible for reading from and writing to ETS tables. However, I also wanted to see what ETS is all about so I threw it into the mix as a learning experience. Generally a way to store some state in Elixir is a GenServer, which can store things and let other processes read and write them. Still, we do need to remember the state of the game while it’s in progress in a way that’s accessible to all players. I knew up-front there will be no need to permanently store any data.

elixir ecto table names plural or singular

  • The pipe operator `|>` might seem strange at first, but you get used to it very quickly.
  • In both cases we call `ensure_leaders()`, which just makes sure that some rule constraints are satisfied after every update.
  • I'm on the fence about the relative importance of being succinct vs. In Elixir, we can use `MapSet`, however, since there are no classes or objects, we call `MapSet.put` explicitly, so it's a bit more verbose. is a `Set` and we can just call its `add` method, which is pretty convenient. Since everything is immutable and we want to make changes, we create new structs.
  • We also return an updated `GameState` struct, though it's a different one (as in, it's stored at a different memory location) than the one we accepted.
  • There is no `self` or anything similar - we're purely operating on arguments and we just accept `game` struct (not "object") as one of our arguments.
  • For instance a method to join a team might be something like:ĭef join(color, user_id) than than, there are a few things going on here: In the Rails world, it would probably be a class with a bunch of attributes and methods to modify them. It has a bunch of attributes: id, the grid of words on the "table", who's on which team, how many points each team has etc. Let's go through a quick example: I store the state in a data structure I call, imaginatively, `GameState`. I was so used to OO and mutability that it took some real effort to switch mindsets. Probably the clearest two differences are functional programming (instead of object-oriented) and immutable data structures. Only once I realized it was a completely different beast and made space in my head for that fact, was I able to learn and appreciate everything properly. And I feel it's important to know this, because I struggled for a while unnecessarily trying to fit what I was learning into what I already knew. I was excited to visit and this world feels increasingly like home - but it is different. While authors of both Elixir and Phoenix came from the Rails community and you can see some inheritance if you look closely enough, it felt like an alien world to me.

    #Elixir ecto table names plural or singular how to#

    Writing Elixir is a totally different experience to Ruby and nothing really carries over in terms of how to structure your app, how to think about solving problems etc. But if you know Rails, abandon any hope of knowing anything about Phoenix, other than "how the web works". Let me tell you, the second half of that sentence is a lie.Įlixir is a nice language, it does compile to Erlang and Phoenix is a web framework for Elixir. I don't know where it was, but the first time I've heard about Phoenix was something like "Oh Elixir is just a nice language, which compiles to Erlang and Phoenix is a Rails-like web framework for Elixir".













    Elixir ecto table names plural or singular