I've got a post model in my Phoenix Framework application.
I'd like to add records through my terminal. In Rails I can do something like this in the rails console:
u = Post.create title: "My Title", content: "Here's my content..."
What is the equivalent to this in IEX?
first of all, you need to start your elixir terminal with
iex -S mix
make you have that -S mix
or not it won't do.
after iex run, you just need to alias your module (to make it easier to access)
alias MyApp.Repo
alias MyApp.Post
after you setup the alias, you are ready to do anything you want. just test it with getting all the post with:
Repo.all(Post)
if there is no error (UndefinedFunctionError) then you can just insert your data with:
Repo.insert(%Post{title: "My Title", content: "Here's my content..."})
hope it will help you. :D
Found the answer in the Phoenix Documentation.
In IEx I can do this:
post = %MyApp.Post{title: "My Title", content: "Here's my content..."}
Followed by:
MyApp.Repo.insert post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With