Problem
I have a files
table and there are many other tables that create a one-to-one association, e.g. users
might have a avatar
and posts
might have photo
.
A possible solution
A possible solution would be to create users_files
and posts_files
tables and use has_one :through
. However, this looks excessive.
The ideal solution
The ideal solution would be to define tables like this
users
- avatar_id
posts
- photo_id
and have with:
parameter in has_one
so the schema looks like this
schema "users" do
has_one :avatar, MyApp.FileDb, with: :avatar_id, foreign_key: :id #id is default
end
schema "posts" do
has_one :photo, MyApp.FileDb, with: :photo_id, foreign_key: :id
end
and that way you don't need to define a belongs_to
on files
. Is there a similar mechanism already? What is the standard way to deal with this in Phoenix?
belongs_to :users
&&belongs_to :posts
this would require users_id and posts_id in the file table.. it's nice for tables like images – Polad