0

I have a User model that has a

has_rich_text :tlk_with_me

rich text blob attached.

On one of my pages I want to load all the users with a tlk_with_me blob, and none of the other users.

I have tried the solution in this answer:

How to query records that have an ActiveStorage attachment?

Which suggests doing the following query:

@users_with_twm_attached = User.joins(:tlk_with_me_attachment)

However I get the following error:

ActiveRecord::ConfigurationError (Can't join 'User' to association named 'tlk_with_me_attachment'; perhaps you misspelled it?)

I am unsure how to proceed. Thank you

JoshuaESummers
  • 493
  • 2
  • 7
  • 24
  • 1
    If you post your table structure for users and their associated blobs we can help – Mark Jan 31 '20 at 15:35
  • @RockwellRice, https://edgeguides.rubyonrails.org/action_text_overview.html the ActionText docs don't suggest adding a column to the table. (see '4 Examples') – JoshuaESummers Jan 31 '20 at 16:56
  • @Mark The User table does not have a relevant column in it (see my answer to Rockwell). If there is a way to get table+blobs, please let me know. – JoshuaESummers Jan 31 '20 at 16:57
  • So, if you output the record, is there a value for `<%= user.tlk_with_me %>` ? You should be able to check on something like `<% if user.tlk_with_me != '' %>`, or something similar, to see if there is a value there. Or, in your controller something like `users = User.all` and then `@users_with_twm = users.select {|t| user.tlk_with_me != ''}` (I'm not exactly sure if that is the exact check it would depend on what an empty value in the DB looks like) – Rockwell Rice Jan 31 '20 at 17:03
  • Thanks @RockwellRice. I want to display ALL the users who have a tlk_with_me rich text attachment, so I feel like if I work with an enumerable going through all of my users, I'm doing a lot of work if say 5% have a 'tlk_with_me' attachment. – JoshuaESummers Jan 31 '20 at 17:27
  • @RockwellRice ActionText actually works a lot like ActiveStorage. It does not rely on any column on the model itself rather everything is stored on the `action_text_rich_texts` table. – max Jan 31 '20 at 18:02

1 Answers1

3

Association for your case will be rich_text_tlk_with_me, thus

User.joins(:rich_text_tlk_with_me)

(and also probably .where('action_text_rich_texts.body!=""') or so)

Vasfed
  • 18,013
  • 10
  • 47
  • 53