nyakio

nyakio

How to update inserted_at timestamps?

I have a list of records that are to be sorted by the inserted_at column, and I would like to test the sorting functionality. In Ecto, I would create a list of the records and use Repo.update to update the inserted_at column with a calculated value so that the records have different timestamps.

I was able to work this out when I made the inserted_at attributed writable and passed it as an attribute to the update action

I would like to have the inserted_at field as a private attribute so that users cannot update it via the form, but this approach does not return updated values.

In my resource I have:

attribute :inserted_at, :utc_datetime_usec do
  writable? true
  default &DateTime.utc_now/0
  match_other_defaults? true
  allow_nil? false
end

...
update :update do
  argument :inserted_at, :utc_datetime_usec

  accept [:email, :language, :creator_id, :tenant_id, :inserted_at]

  primary? true
end

And In the test I have:

for invitation <- invitations, i <- 1..length(invitations) do
  invitation
  |> Ash.Changeset.for_update(
      :update,
      %{},
        private_arguments: %{inserted_at: time_before_or_after(-i * 12000)},
      authorize?: false
    )
    |> Ash.update!()
end

Most Liked

barnabasJ

barnabasJ

Ash Core Team

Hi

You still need to do something with the argument. That’s probably not precisely clear just by reading the Privat Inputs section of the docs. But an argument by itself is not doing anything.

update :update do
  argument :inserted_at, :utc_datetime_usec

  accept [:email, :language, :creator_id, :tenant_id, :inserted_at]

  primary? true

  change fn changeset, _context -> 
    case Ash.Changeset.get_argument(changeset, :inserted_at) do
      nil -> changeset
      datetime -> Ash.Changeset.change_atttrribute(changeset, :inserted_at, datetime)
    end
  end
end
barnabasJ

barnabasJ

Ash Core Team

I would probably go with the context route described in the next section of the Docs.
For example, if you use AshGraphql, the argument would appear in the API. You could set it to public?: false to hide it from that layer though. But then you don’t have a good way to supply it yourself if needed.

But if you go the context route, you could supply values to AshGraphl in a plug or middleware that would be passed on to the actions.

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement