0

I've got an Organization model and User model. Organization belongs to User with :creator alias:

class Organization < ApplicationRecord
  belongs_to :creator, class_name: 'User', optional: true
end

User model is based upon devise with confirmation by email required. It has .confirm? method which returns boolean value.

So I want to filter all organizations with creators who have confirmed accounts. But when I try something like this:

Organization.find_by(creator: creator.confirmed?) 

I get this error:

NameError (undefined local variable or method `creator' for main:Object)

How can I filter Organizations based on attributes of its creators?

andrzej541
  • 919
  • 1
  • 9
  • 21
  • Does this answer your question? [Rails: ActiveRecord query based on association value](https://stackoverflow.com/questions/19489017/rails-activerecord-query-based-on-association-value) – dbugger Dec 17 '20 at 13:03

1 Answers1

0

According to the source code of devise confirmable module, the method confirm? will return true if the column confirmed_at is set to not null.

  # Verifies whether a user is confirmed or not
  def confirmed?
    !!confirmed_at
  end

So, You can get the confirmed users by querying the users with confirmed_at is not null:

Try the below to get the organization created by unconfirmed users:

Organization.joins(:creator).where('users.confirmed_at IS NOT NULL')
user11350468
  • 1,357
  • 1
  • 6
  • 22