有什么方法可以通过选择另一个联接的列来预加载记录?
# table structure
# User 1---* Post 1---* PostTag *---1 Tag
# extract definition of scheme
scheme "posts" do
...
has_many :post_tags, PostTag
has_many :tags, [:post_tags, :tag]
end
Run Code Online (Sandbox Code Playgroud)
以下伪代码表达了我的目标(但不起作用)。
query = from post in Post,
join: user in User, on post.user_id == user.id,
select: %{
id: post.id,
title: post.title,
user_name: user.name, # <= column at joined table
},
preload: [:tags]
Repo.all(query)
#=> ** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query:` …
Run Code Online (Sandbox Code Playgroud) 我可以定义与模块共享的常量吗?
通常,我们使用模块属性作为模块内部的常量。 http://elixir-lang.github.io/getting-started/module-attributes.html#as-constants
如果我想让其他模块可以使用常量,我该怎么做?起初,module 属性仅在 module(1) 内部可用。我知道我可以将函数用作常量(2)。但是如果可能的话,我想通过像 3 这样的专用形式来定义常量。
defmodule Sample do
# 1. module attribute(available only inside module)
@private_const_value 1
# 2. function as constant(possible in any rate)
def const_value_func, do: 3
# 3. ideal form(but compile error)
public_const_value = 2
end
defmodule Client do
def foo() do
IO.puts "CONST = #{Sample.@private_const_value}" # compile error
IO.puts "CONST = #{Sample.const_value_func}"
IO.puts "CONST = #{Sample.public_const_value}" # compile error
end
end
Run Code Online (Sandbox Code Playgroud)
我希望常量满足以下要求。