В Twitter как приложение, где User
может отслеживать другого пользователя User
через модель Connection
У меня проблемы с отображением, если Follower
получает обратно @user
.Как определить, включен ли элемент ассоциации has_many в другой?
веб/модели/user.ex
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :last_name, :string
has_many :follower_connections, MyApp.Connection, foreign_key: :followee_id
has_many :followers, through: [:follower_connections, :follower]
has_many :followee_connections, MyApp.Connection, foreign_key: :follower_id
has_many :followees, through: [:followee_connections, :followee]
[...]
веб/модели/connection.ex
defmodule MyApp.Connection do
use MyApp.Web, :model
schema "connections" do
belongs_to :follower, MyApp.User
belongs_to :followee, MyApp.User
[...]
веб/контроллеры/user_controller.ex
[...]
def show(conn, %{"id" => id}) do
user =
Repo.get!(User, id)
|> Repo.preload([:followers, :followees,
:followee_connections,
:follower_connections])
conn
|> assign(:user, user)
|> render("show.html")
end
[...]
веб/шаблоны/пользователь/show.html.eex
<ul>
<%= for connection <- @user.follower_connections do %>
<li>
<%= connection.follower.last_name %>
<%= if connection.followee.???include?(????) do %>
(You follow him/her back.)
<% end %>
</li>
<% end %>
</ul>
Как я могу узнать, если connection.follower
является частью или включены в @user.followees
?
Бонусный вопрос: где это в документации Ecto?