AF
Ash Framework•4mo ago
Failz

code_interface, compile time vs runtime

if this were a code_interface, is timestamp captured once at compile time and all future calls use that compiled timestamp value? If yes, is there a different way to solve this? I've got multiple live views calls this code_interface and I'm wanting to keep all the logic in one place
define :get_budget_with_accounts,
action: :read,
get_by: [:id],
default_options: [
load: [
:total_balance,
accounts:
App.Finance.Account
|> Ash.Query.load(:balance_as_of, timestamp: DateTime.utc_now())
|> Ash.Query.sort(:identifier)
]
]
define :get_budget_with_accounts,
action: :read,
get_by: [:id],
default_options: [
load: [
:total_balance,
accounts:
App.Finance.Account
|> Ash.Query.load(:balance_as_of, timestamp: DateTime.utc_now())
|> Ash.Query.sort(:identifier)
]
]
Solution:
i.e default_options: fn -> ... end
Jump to solution
7 Replies
ZachDaniel
ZachDaniel•4mo ago
timestamps would be captured at compile time The only way to solve it would be to make it also accept a function.
Solution
ZachDaniel
ZachDaniel•4mo ago
i.e default_options: fn -> ... end
Failz
FailzOP•4mo ago
compile time, got it. I was seeing weird behavior with balances and suspected that might be it. Thank you!
ZachDaniel
ZachDaniel•4mo ago
This would likely be a very easy PR to make if you're interested 😄 with that said, if that is coming from ash_double_entry I think it defaults to now inside the calculation?
Failz
FailzOP•4mo ago
i left off timestamp: DateTime.utc_now() and saw the same behavior.
ZachDaniel
ZachDaniel•4mo ago
hmm... oh yeah thats a weird one because it sets the defaults when you load
define :get_budget_with_accounts,
action: :read,
get_by: [:id],
default_options: [
load: [
:total_balance,
accounts: Ash.Query.sort(App.Finance.Account, :identifier),
accounts: [:balance_as_of]
]
]
define :get_budget_with_accounts,
action: :read,
get_by: [:id],
default_options: [
load: [
:total_balance,
accounts: Ash.Query.sort(App.Finance.Account, :identifier),
accounts: [:balance_as_of]
]
]
but yeah making that accept a function would be good.

Did you find this page helpful?