Hi i have a table “activities” in my db with colums: id, user_id, user_to_id, photo_id, type
type can be: ‘vote’, ‘user_follow’, ‘photo_follow’
I created a schema Vote, UserFollow, PhotoFollow.
The schema of Vote is like this:
defmodule Shuttertop.Votes.Vote do
@moduledoc false
use TypedEctoSchema
import Ecto.Changeset
require Shuttertop.Constants
alias Shuttertop.Accounts.User
alias Shuttertop.Constants, as: Const
alias Shuttertop.Photos.Photo
typed_schema "activities" do
field(:type, :integer, default: Const.action_vote)
belongs_to(:photo, Photo)
belongs_to(:user, User)
timestamps()
end
...
The Photo schema is like this:
defmodule Shuttertop.Photos.Photo do
@moduledoc false
use TypedEctoSchema
import Ecto.Changeset
alias Shuttertop.Accounts.User
alias Shuttertop.Votes.Vote
typed_schema "photos" do
field(:name, :string, null: true)
field(:slug, :string, null: false)
has_many(:votes, Vote, on_delete: :delete_all)
...
when i do a select like:
from(
p in Photo,
preload: [:user, :votes]
)
the rows in photo.votes contains also PhotoFollow type
How can i get only Vote type without specify it in filter?
Now i can do it with this code, but i don’t like it:
from(
p in Photo,
distinct: true,
left_join: v in Vote,
on: [photo_id: p.id, type: ^Const.action_vote],
preload: [:user, votes: v]
)
Thanks