I have a `dancer` has many couples and
# avo-2
m
I have a
dancer
:has_many couples and has a
profile_image
From the couple I would like to display the dancer image. Is there a reference somewhere on how to do something like this? 🤔
l
hmmm
not sure I understand
can you show some code?
I dont' understand the associations
m
Copy code
ruby
# frozen_string_literal: true

class CoupleResource < Avo::BaseResource
  self.title = :dancer_names
  self.includes = [:dancer, :partner, :videos]
  self.search_query = -> do
    scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
  end

  field :id, as: :id
  field :dancer, as: :belongs_to
  field :dancer_image, as: :file, is_image: true, is_avatar: true do
    model.dancer.profile_image
  end
  field :partner, as: :belongs_to
  field :partner_image, as: :file, is_image: true, is_avatar: true, format_using: ->(model) { model.partner.profile_image }
  field :videos_count, as: :number do
    model.videos.length
  end
  field :slug, as: :text
  field :unique_couple_id, as: :text, hide_on: [:index, :show]
  field :couple_videos, as: :has_many
  field :videos, as: :has_many, through: :couple_videos
end
Copy code
ruby
class Dancer < ApplicationRecord
  belongs_to :user, optional: true
  has_many :dancer_videos, dependent: :destroy
  has_many :videos, through: :dancer_videos
  has_many :orchestras, through: :videos
  has_many :songs, through: :videos

  has_many :couples, dependent: :destroy
  has_many :partners, through: :couples
  has_many :performances, through: :videos
  has_one_attached :profile_image
  has_one_attached :cover_image
Copy code
ruby
class Couple < ApplicationRecord
  belongs_to :dancer
  belongs_to :partner, class_name: "Dancer"
  has_many :couple_videos, dependent: :destroy
  has_many :videos, through: :couple_videos
l
and where do you want to display it?
on which resource?
m
on my couples resource here as avatars.
l
Copy code
ruby
field :photo, as: :file do |model|
  model.dancer.profile_image
end
m
Copy code
ruby
field :dancer_image, as: :file, is_image: true, is_avatar: true do
    model.dancer.profile_image
  end
something like this?
l
yes! preciselly
m
Copy code
web    |
web    |
web    |
web    | ActiveRecord::AssociationNotFoundError (Association named 'dancer_image_attachment' was not found on Couple; perhaps you misspelled it?):
web    |
web    | rails (d137b10f946f) activerecord/lib/active_record/associations.rb:302:in `association'
web    | rails (d137b10f946f) activerecord/lib/active_record/associations/preloader/branch.rb:79:in `block in grouped_records'
web    | rails (d137b10f946f) activerecord/lib/active_record/associations/preloader/branch.rb:77:in `each'
web    | rails (d137b10f946f) activerecord/lib/active_record/associations/preloader/branch.rb:77:in `grouped_records'
web    | rails (d137b10f946f) activerecord/lib/active_record/associations/preloader/branch.rb:114:in `loaders'
web    | rails (d137b10f946f) activerecord/lib/active_record/associations/preloader/branch.rb:71:in `runnable_loaders'
l
I don't see the
has_one_attahced
on dancer
m
I left it out when I copy pasted
But it's there.
and I'm displaying it on my dancer index
l
sorry, but I don't know that
it's hard to follow without code
... or different code that you're seeing
this is the repo
l
this is not right
the block at the end takes some params
do |model|
the no-params version is in Avo 3
if you want to follow the avodemo repo, use this tag which uses Avo 2 https://github.com/avo-hq/main.avodemo.com/tree/pre-avo-3
TBH, I don't think this error comes from the
field :dancer_image, as: :file, is_image: true, is_avatar: true do
field
this seems to work without the
do |model|
arguments. weird
can you share all of the stack trace please?
l
ok. let me test it
ok. I see the same error on my machine
investigating
pushing a fix in a few minutes
m
I didn't know that developers can be so AWESOME 🙃
l
nah. it's a stupid optimization bug
sorry for grilling you about the specifics
it's difficult to debug scenarios like this
m
no worries man! I'm glad to use the product and I'm glad I can help contribute to make it better.
l
m
Is this the correct way to reference it in my gemfile?
gem "avo", git: "https://github.com/avo-hq/avo.git", branch: "fix/stop-eager-loading-computed-file-fields", ref: "1789"
I haven't pulled a specific PR before
l
I think you don't need the
ref
I'm cutting a new tag and releasing in a few minutes
m
l
run
bin/rails avo:build-assets
m
Cool! thanks! it's working!
l
and, is the bug fixed? can you see the image?
m
yes It's working
hahas sorry. I should have said it's working 2x
;P
l
perfect! thanks for the help
m
It could be useful to explain how to include (eager load) file images on a resource.
l
that was the bug 😛
Avo does it automatically
for regular file fields
this is the bit of code that does that automatically
m
It would be good to say that then. Otherwise the N+1 spidey sense might be going off for people.
l
yeah... I don't know where to say that 😛
Avo does so many things
releasing
2.34.6
in a second with these fixes