Undefined method `data' (NoMethodError)

I’m trying to retrieve data from these format of SQL but it shows the error code "Puma caught this error: undefined method `data’ (NoMethodError)

module Reports
  class GenerateSeriatimReportNewExcel
    def initialize(as_of:, branch:)
      @as_of = as_of
      @branch = branch
      @valid_members = []

      if @as_of.present? && @branch.present?
        if Settings.activate_microloans
            def query_members!
              ActiveRecord::Base.connection.execute(<<-EOS).to_a
                SELECT
                    a.*, b.name AS branch_name
                  FROM members a
                  LEFT JOIN branches b ON a.branch_id = b.id
                  WHERE a.data ->> 'recognition_date' <= '#{@as_of.to_date}'
                  AND a.member_type != 'GK'
                  AND a.insurance_status NOT IN ('resigned', 'pending')
                  AND a.status IN ('active')
                  AND a.branch_id = '#{@branch}'
                  ORDER BY a.insurance_status ASC
              EOS
            end
            @members = query_members!

            def query_resigned!
              ActiveRecord::Base.connection.execute(<<-EOS).to_a
                SELECT
                    a.*, b.name AS branch_name
                  FROM members a
                  LEFT JOIN branches b ON a.branch_id = b.id
                  WHERE a.data ->> 'recognition_date' <= '#{@as_of.to_date}'
                  AND a.insurance_date_resigned > '#{@as_of.to_date}'
                  AND a.insurance_status IN ('resigned')
                  AND a.branch_id = '#{@branch}'
                  ORDER BY a.insurance_status ASC
              EOS
            end
            @resigned = query_resigned!
            @valid_members = @members + @resigned

but when I change the format of def query_members! from these syntax of SQL below, it works. I just want the same syntax above

 @members = Member.joins(:branch)
                    .select("members.*, branches.name AS branch_name")
                    .where("data->>'recognition_date' <= ?", @as_of.to_date)
                    .where.not(member_type: "GK")
                    .where.not(insurance_status: ["resigned", "pending"])
                    .where(status: "active")
                    .where(branch_id: @branch)
                    .order("insurance_status ASC")

 def query_resigned!
              ActiveRecord::Base.connection.execute(<<-EOS).to_a
                SELECT
                    a.*, b.name AS branch_name
                  FROM members a
                  LEFT JOIN branches b ON a.branch_id = b.id
                  WHERE a.data ->> 'recognition_date' <= '#{@as_of.to_date}'
                  AND a.insurance_date_resigned > '#{@as_of.to_date}'
                  AND a.insurance_status IN ('resigned')
                  AND a.branch_id = '#{@branch}'
                  ORDER BY a.insurance_status ASC
              EOS
            end
            @resigned = query_resigned!
            @valid_members = @members + @resigned

Can anyone help me to fix this error ?