Has many through many fields

I’m really confused at what you want. You want a true/false indicator in a list of posts you already have? Then either be really inefficient and keep what you have and just set to true if the count of results from get_favourites is 1, else false. Or else run a 2nd query to find all the user’s favourites and set a key on each matching post via an Enum.map

@gregvaughn:
True/false indicator would be the way, but I don’t know how to achieve this. I would be appreciated for help.

And second option (with 2nd query) was posted above, but I still don’t know, how to join single post from posts list with that sql result.

@NobbZ:

I’m really thankful for your attention.

Ok, let’s make it simplier. How to get in each post information if post is favourite or not? Let’s use virtual field:

field :is_favourite, :integer, virtual: true

But how to set this? At first we need to check if user is logged, so if (Guardian.Plug.current_resource(conn)). Can I use Guardian.Plug.current_resource(conn) in model?
And later on, how to achieve is_favourite information?

Since the field is now part of your struct, you can use update syntax to set its value:

post = %{post | field: 1}

You can read that value by doing simple field access:

value = post.is_favourite

I’m not sure though, what Guardian has to do with this…


edit

I do consider the name (is_favourite) confusing for an integer field. In elixir the prefix is_ and the suffix ? are usually used for booleans, while the prefix is_ is meaning “this is guardsafe”.

Use left_join maybe?

1 Like

@NobbZ

I’m not sure though, what Guardian has to do with this…

If it is wrong, tell me how to get logged user data instead of Guardian? Mayby I’m wrong and there is simplier method.

You are right about boolean. So let’s assume:

field :is_favourite, :boolean, virtual: true

Still don’t have any idea how to iterate through posts to set this field true or false for each post. How can I achieve this?

@LostKobrakai
You are right. But with that code:
In posts_controller:

current_user = Guardian.Plug.current_resource(conn)
page = Post.preload
|> Post.order_asc_by_insertion
|> Post.getFavourites2(current_user.id)
|> Repo.paginate(params)

And in model:

def getFavourites2(query, user_id) do
from p in query,
left_join: f in “posts_favourites”, on: f.post_id == p.id and f.user_id == ^user_id
end

I don’t have posts_favourites in output.

Where do you store your logged in user data? I just use the build-in Phoenix.Token on the Plug.Session. Guardian is more useful for DB-less and remote API endpoints, not for a main website (it is significantly heavier than Phoenix.Token).

@OvermindDL1

Actually, I use it for API. I don’t have any views. I authenticate user by JWT. So I store all logged in user data in:

Guardian.Plug.current_resource(conn)

Views doesn’t matter.

In essence:
Is the remote server needing to authenticate with your server then Guardian is nice.
Are you auth’ing local, then Phoenix.Token is better.

You haven’t told anything about a logged in user before, anyway, that really doesn’t matter for your problem right now. We really have to keep it at a really basic level and should not overcomplicate it.

The main problems you have are iterating over a list while transforming each item into a list of separate results and how to deal with structs.

I have shown you how to do both in separate posts as you slowly uncovered your requirements. In germany we call this “salami tactics”, giving out stuff slice by slice…

In one post I introduced Enum.map/2 to you, in another I introduced the map/struct-update syntax. Combining those, as both are basic building blocks of elixir, is left as an exercise to you.