my model looks like
class User < ApplicationRecord
has_rich_text :profile_text
end
Now I am trying to find all the records that have an empty profile_text
my model looks like
class User < ApplicationRecord
has_rich_text :profile_text
end
Now I am trying to find all the records that have an empty profile_text
First, take a look at the action-text table
create_table :action_text_rich_texts do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false
t.timestamps
t.index [ :record_type, :record_id, :name ],
name: "index_action_text_rich_texts_uniqueness",
unique: true
end
in which, name
is corresponding to the rich-text attribute (in your case, it's profile_text
), record
(type and id) are corresponding to your class User
.
So in my opinion, one way to go is to pluck record_id
s from all action-text which has body present, and the complement records which not in this record_id
s are corresponding to records (empty or nil) you want to query.
scope :without_rich_text, -> {
ids = ActionText::RichText
.where(name: 'profile_text', record_type: 'User')
.where.not(body: [nil, ""])
.pluck(:record_id)
where.not(id: ids)
}
User.without_rich_text
User.without_rich_text.first
User.where(department: [1,2,3]).without_rich_text
Note that iam trying to cover the case there're users in which profile_text
is not existed in the action-text
table at all, so i query with negative case where.not
.
if you're sure that all your User profile_text
existed in the action-text
table and you just want to filter all empty records, then you could query with positive case
scope :without_rich_text, -> {
ids = ActionText::RichText
.where(name: 'profile_text', record_type: 'User')
.where(body: [nil, ""])
.pluck(:record_id)
where(id: ids)
}