Working with Ecto: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:




https://brooklinmyers.medium.com/ecto-with-phoenix-in-4-minutes-9b7c447055c6
Working with ECTO in IEX can be confusing. These examples below are performed with a table named Testbeds created using the Phoenix Generator. The functions referenced are in app/testbeds.ex where '''app''' is the name of your application and '''testbeds.ex''' is the name of your table.
 
Make sure you are aware of camel casing.




==Update all tables that contain a field with a chosen value==


<source>
  def reset_testbeds do
    items = from t in TestBed, where: t.status == "Taken"
    Repo.update_all(items, set: [status: "Available", developer: "None"])
    |> broadcast_change([:testbed, :reset])
  end
 


</source>


==IEX Commands (with example data)==
==IEX Commands (with example data)==


===Get All ===
===Get All ===


<source>
<source>
App.TestBeds.TestBed |> App.Repo.all  
App.Testbeds.Testbed |> App.Repo.all  
</source>
</source>


Line 30: Line 27:


<source>
<source>
%App.TestBeds.TestBed{name: "some-item-name"} |> App.Repo.insert
%App.Testbeds.Testbed{name: "some-item-name"} |> App.Repo.insert


</source>
</source>
==Update all tables that contain a field with a chosen value==
<source>
  def reset_testbeds do
    items = from t in Testbed, where: t.status == "Taken"
    Repo.update_all(items, set: [status: "Available", developer: "None"])
    |> broadcast_change([:testbed, :reset])
  end
 
</source>
==References==
https://brooklinmyers.medium.com/ecto-with-phoenix-in-4-minutes-9b7c447055c6

Latest revision as of 00:21, 7 October 2023

This page is in progress


Working with ECTO in IEX can be confusing. These examples below are performed with a table named Testbeds created using the Phoenix Generator. The functions referenced are in app/testbeds.ex where app is the name of your application and testbeds.ex is the name of your table.

Make sure you are aware of camel casing.



IEX Commands (with example data)

Get All

App.Testbeds.Testbed |> App.Repo.all


Insert

%App.Testbeds.Testbed{name: "some-item-name"} |> App.Repo.insert





Update all tables that contain a field with a chosen value

   def reset_testbeds do
    items = from t in Testbed, where: t.status == "Taken"
    Repo.update_all(items, set: [status: "Available", developer: "None"])
    |> broadcast_change([:testbed, :reset])
  end

References

https://brooklinmyers.medium.com/ecto-with-phoenix-in-4-minutes-9b7c447055c6