bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

ActiveStorage attachment is present, but showing `nil` in Datagrid column #282

Closed rhunals closed 4 years ago

rhunals commented 4 years ago

user.rb

class User 
  has_one_attached :avatar
end

Inspected the same user in rails console @user.avatar.attached? => true

but displaying false for all users in datagrid column.

class UserGrid

  scope  do
    User.joins(:avatar_attachment)
  end

  column(:avatar, html: true, header: 'Avatar') do |record|
    if record.avatar.attached?
      concat link_to(
        'image_tag(url_for(record.avatar))',
        record.avatar_url,
        target: '_blank'
      )
    end
  end
end

Inspected record.avatar.attached? from datagrid column, getting false

Requirement: Display all User records that have an attachment and also display attachment in grid column.

Issue: As attachment is showing nil, so unable to display attachment in grid.

bogdan commented 4 years ago

It is rarely the case for datagrid to have such issues.

Does the following return true for the required record?

User.joins(;avatar_attachment).find(...).avatar.attached?
rhunals commented 4 years ago

It is rarely the case for datagrid to have such issues.

Does the following return true for the required record?

User.joins(;avatar_attachment).find(...).avatar.attached?

returns true.

There is an issue in scope in this query User.joins(:avatar_attachment)

here, I am getting record of user, which is correct, but getting wrong record.id, which is not of that user. column(:avatar, html: true, header: 'Avatar') do |record| record.id => 61

User.joins(:avatar_attachment).pluck(:id) => [33, 41, 42, 53, 57]

bogdan commented 4 years ago

What is the query generated by User.joins(:avatar_attachment).pluck(:id) and by datagrid when you call UserGrid.new.assets ?

bogdan commented 4 years ago

No feedback, closing.